Hunter Henrichsen

Hunter Henrichsen

Search Circle
< lecture

Lecture 18 - Deployment Final

posted over 1 year ago 3 min read

Lecture 18 - Deployment Final#

Housekeeping#

News#

Deploying a Lambda Container#

Here’s my basic project setup (also available here):

Terminal window
mkdir my-lambda
cd my-lambda
npm init -f
npm i -D esbuild typescript eslint prettier
touch src/index.ts
touch 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 builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM amazon/aws-lambda-nodejs:18
COPY --from=builder /app/dist/index.js ./
COPY package*.json ./
RUN npm install --production
CMD [ "index.handler" ]

Now to build the container and run it locally:

Terminal window
docker build . -t my-lambda
docker run --rm -p 8080:8080 my-lambda

And to test it locally:

Terminal window
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:

Terminal window
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
Terminal window
aws ecr get-login-password --region us-west-1 | docker login --username AWS --password-stdin XXXXXXXXXXXX.dkr.ecr.us-west-1.amazonaws.com/my-lambda
docker tag my-lambda:latest XXXXXXXXXXXX.dkr.ecr.us-west-1.amazonaws.com/hhenrichsen-ecr/my-lambda:latest
docker push XXXXXXXXXXXX.dkr.ecr.us-west-1.amazonaws.com/hhenrichsen-ecr/my-lambda:latest
Terminal window
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
Terminal window
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:

Reading#

None! Q&A Session next time. Find something interesting to share for the News section next time.