Lecture 18 - Deployment Final
posted over 1 year ago ⋄
3 min read
Lecture 18 - Deployment Final#
Housekeeping#
- LearningSuite is published; I forgot to hit publish last week but it’s open as of Monday.
- Please use [email protected] to communicate with me over email; the university has asked that all email communications I do with you all are done via that email.
News#
- Neuralink does their first human trial
- DeepMind AlphaDev discovers faster sorting algorithms
- Italy tells OpenAI that ChatGPT violates EU privacy regulations
Deploying a Lambda Container#
Here’s my basic project setup (also available here):
mkdir my-lambdacd my-lambdanpm init -fnpm i -D esbuild typescript eslint prettiertouch src/index.tstouch tsconfig.json
tsconfig.json
{ "compilerOptions": { "target": "es2020", "strict": true, "preserveConstEnums": true, "noEmit": true, "sourceMap": false, "module": "commonjs", "moduleResolution": "node", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "isolatedModules": true }, "include": ["src/**/*"], "exclude": ["node_modules", "**/*.test.ts"]}
package.json
{ "name": "my-lambda", "version": "1.0.0", "main": "src/index.ts", "private": true, "scripts": { "build": "tsc && esbuild src/index.ts --bundle --minify --platform=node --outfile=dist/index.js" }, "keywords": [] // ...}
src/index.ts
export const handler = async (event: any, context: any) => { console.log(event); console.log(context); return "Hello World!";};
Dockerfile
FROM node:18 as builderWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .RUN npm run build
FROM amazon/aws-lambda-nodejs:18COPY --from=builder /app/dist/index.js ./COPY package*.json ./RUN npm install --productionCMD [ "index.handler" ]
Now to build the container and run it locally:
docker build . -t my-lambdadocker run --rm -p 8080:8080 my-lambda
And to test it locally:
aws lambda invoke \--region us-west-1 \--endpoint http://localhost:8080 \--no-sign-request \--function-name function \--cli-binary-format raw-in-base64-out \--payload '{"a":"b"}' output.txt
Now to deploy it:
aws iam create-role --role-name lambda-ex --assume-role-policy-document '{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}] }'
arn:aws:iam::778144397950:role/lambda-ex
aws ecr get-login-password --region us-west-1 | docker login --username AWS --password-stdin XXXXXXXXXXXX.dkr.ecr.us-west-1.amazonaws.com/my-lambdadocker tag my-lambda:latest XXXXXXXXXXXX.dkr.ecr.us-west-1.amazonaws.com/hhenrichsen-ecr/my-lambda:latestdocker push XXXXXXXXXXXX.dkr.ecr.us-west-1.amazonaws.com/hhenrichsen-ecr/my-lambda:latest
aws lambda create-function \--package-type Image \--function-name lambda-docker-hello-world \--role arn:aws:iam::XXXXXXXXXXXX:role/lambda-ex \--code ImageUri=XXXXXXXXXXXX.dkr.ecr.us-west-1.amazonaws.com/hhenrichsen-ecr/lambda-my-lambda:latest
aws lambda \--region us-west-1 invoke \--function-name my-lambda \--cli-binary-format raw-in-base64-out \--payload '{"a":"b"}' \output.txt
SST (Serverless STack) and a Simple Image Host#
We’ll be talking about this repo.
SST is fairly approachable, but is JS-only. It generates code to allow you to use your infrastructure in a typesafe way, and includes developer tooling. I think it’s pretty cool.
There are other tools like this, including:
- Serverless Framework - mainly focused on lambda creation and deployment
- Terraform - lower level, deals with creating individual resources as code
- Amazon CDK
Reading#
None! Q&A Session next time. Find something interesting to share for the News section next time.