How do I deploy the ubuntu:20.04 docker image on AWS Lambda with TypeScript?
Overview
In this guide, we will walk through the process of deploying an ubuntu:20.04
Docker image to AWS Lambda using TypeScript. This setup allows you to run an AWS Lambda function that executes within an Ubuntu container environment.
Resources Utilized
- AWS Lambda: We’ll use AWS Lambda to run our containerized application.
- AWS ECR (Elastic Container Registry): This service stores, manages, and deploys Docker container images.
- IAM Role: An AWS Identity and Access Management (IAM) role will be created for Lambda to access other AWS resources securely.
Key Points
- We will push the
ubuntu:20.04
Docker image to AWS ECR. - We’ll define an AWS IAM role that the Lambda function will assume.
- Create the Lambda function using the container image from ECR.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// ECR Repository for Docker image
const ubuntuRepository = new aws.ecr.Repository("ubuntu_repository", {name: "ubuntu-repository"});
// IAM Role for Lambda
const lambdaExecRole = new aws.iam.Role("lambda_exec_role", {
name: "lambda-exec-role",
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: {
Service: "lambda.amazonaws.com",
},
}],
}),
managedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"],
});
// ECR Image
const ecrPolicy = new aws.ecr.LifecyclePolicy("ecr_policy", {
repository: ubuntuRepository.name,
policy: `{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 14 days",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["latest"],
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}
`,
});
// Lambda Function
const ubuntuLambda = new aws.lambda.Function("ubuntu_lambda", {
packageType: "Image",
imageUri: pulumi.interpolate`${ubuntuRepository.repositoryUrl}:latest`,
name: "ubuntuLambda",
role: lambdaExecRole.arn,
imageConfig: {
commands: [
"bin/bash",
"-c",
"echo 'Hello, World!'",
],
},
});
export const lambdaFunctionName = ubuntuLambda.name;
export const ecrRepositoryUrl = ubuntuRepository.repositoryUrl;
Summary
We’ve successfully created a setup to deploy an ubuntu:20.04
Docker image to AWS Lambda using TypeScript. We:
- Created an ECR repository for storing the Docker image.
- Defined an IAM role for the Lambda function.
- Created the Lambda function to use the Docker image from the ECR repository.
These foundational blocks ensure that your Lambda function runs seamlessly within the Ubuntu container environment.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.