diff --git a/aws-ts-containers-dockerbuild/Pulumi.yaml b/aws-ts-containers-dockerbuild/Pulumi.yaml new file mode 100644 index 000000000..d2b3644ea --- /dev/null +++ b/aws-ts-containers-dockerbuild/Pulumi.yaml @@ -0,0 +1,10 @@ +name: ${PROJECT} +description: ${DESCRIPTION} +runtime: nodejs + +template: + description: An example that builds a Docker image and deploys a container to AWS Fargate. + config: + aws:region: + description: The AWS region to deploy into + default: us-west-2 diff --git a/aws-ts-containers-dockerbuild/README.md b/aws-ts-containers-dockerbuild/README.md new file mode 100644 index 000000000..b9adbc37f --- /dev/null +++ b/aws-ts-containers-dockerbuild/README.md @@ -0,0 +1,46 @@ +# Deploys a container with a Docker Build image on AWS Fargate + +Deploys a AWS Fargate service. The service uses a Docker image that is build with the new Docker Build provider. The image is pushed to AWS ECR. + +Last revision: July 2024. + +## ๐Ÿ“‹ Pre-requisites + +- [Pulumi CLI](https://www.pulumi.com/docs/get-started/install/) +- *Recommended* [Pulumi Cloud account](https://app.pulumi.com/signup) +- [npm](https://www.npmjs.com/get-npm) +- AWS account and credentials configured + +## ๐Ÿ‘ฉโ€๐Ÿซ Get started + +This Pulumi example is written as a template. It is meant to be copied via `pulumi new` as follows: + +```bash +pulumi new https://github.com/pulumi/examples/tree/master/aws-ts-containers-dockerbuild +npm install +``` + +Once copied to your machine, feel free to edit as needed. + +## ๐ŸŽฌ How to run + +To deploy your infrastructure, run: + +```bash +$ pulumi up +# select 'yes' to confirm the expected changes +# wait a bit for everything to get deployed +# ... +# confirm your service is up and running +$ curl $(pulumi stack output url) +# ๐ŸŽ‰ Ta-Da! +``` + +## ๐Ÿงน Clean up + +To clean up your infrastructure, run: + +```bash +$ pulumi destroy +# select 'yes' to confirm the expected changes +``` diff --git a/aws-ts-containers-dockerbuild/app/Dockerfile b/aws-ts-containers-dockerbuild/app/Dockerfile new file mode 100644 index 000000000..4f9d60fb7 --- /dev/null +++ b/aws-ts-containers-dockerbuild/app/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx +COPY content /usr/share/nginx/html diff --git a/aws-ts-containers-dockerbuild/app/content/favicon.png b/aws-ts-containers-dockerbuild/app/content/favicon.png new file mode 100644 index 000000000..ad4baeb6f Binary files /dev/null and b/aws-ts-containers-dockerbuild/app/content/favicon.png differ diff --git a/aws-ts-containers-dockerbuild/app/content/index.html b/aws-ts-containers-dockerbuild/app/content/index.html new file mode 100644 index 000000000..4081eb79c --- /dev/null +++ b/aws-ts-containers-dockerbuild/app/content/index.html @@ -0,0 +1,7 @@ + + + Hello, Pulumi! + +

Hello, containers!

+

Made with โค๏ธ with Pulumi and deployed to Fargate

+ \ No newline at end of file diff --git a/aws-ts-containers-dockerbuild/index.ts b/aws-ts-containers-dockerbuild/index.ts new file mode 100644 index 000000000..3c6278a3d --- /dev/null +++ b/aws-ts-containers-dockerbuild/index.ts @@ -0,0 +1,91 @@ +// Copyright 2024, Pulumi Corporation. All rights reserved. + +// Import required libraries +import * as aws from "@pulumi/aws"; // Required for ECS +import * as awsx from "@pulumi/awsx"; +import * as dockerBuild from "@pulumi/docker-build"; +import * as pulumi from "@pulumi/pulumi"; // Required for Config and interpolation + +// Read the current stack configuration, see Pulumi..yaml file +// Configuration values can be set with the pulumi config set command +// OR by editing the Pulumi..yaml file. +// OR during the pulumi new wizard. +const config = new pulumi.Config(); + +// An ECS cluster to deploy into. +const cluster = new aws.ecs.Cluster("cluster", {}); + +// An ALB to serve the container endpoint to the internet. +const loadbalancer = new awsx.lb.ApplicationLoadBalancer("loadbalancer", {}); + +// An ECR repository to store our application's container image. +const ecr = new awsx.ecr.Repository("repo", { + forceDelete: true, +}); + +// Grab auth credentials for ECR. +const auth = aws.ecr.getAuthorizationTokenOutput({ + registryId: ecr.repository.registryId, +}); + +// Build and publish our application's container image from ./app to the ECR repository. +const image = new dockerBuild.Image("image", { + + // Use the pushed image as a cache source. + cacheFrom: [{ + registry: { + ref: pulumi.interpolate`${ecr.repository.repositoryUrl}:cache`, + }, + }], + cacheTo: [{ + registry: { + imageManifest: true, + ociMediaTypes: true, + ref: pulumi.interpolate`${ecr.repository.repositoryUrl}:cache`, + }, + }], + + // Build multi-platforms + platforms: [ + dockerBuild.Platform.Linux_amd64, + // add more as needed + ], + push: true, + // Provide our ECR credentials. + registries: [{ + address: ecr.repository.repositoryUrl, + password: auth.password, + username: auth.userName, + }], + // + // Other parameters + // + // Tag our image with our ECR repository's address. + tags: [pulumi.interpolate`${ecr.repository.repositoryUrl}:latest`], + // The Dockerfile resides in the app directory for this example. + context: { + location: "app", + }, +}); + +// Deploy an ECS Service on Fargate to host the application container. +const service = new awsx.ecs.FargateService("service", { + cluster: cluster.arn, + assignPublicIp: true, + taskDefinitionArgs: { + container: { + name: "service-container", + image: image.ref, + cpu: 128, + memory: 512, + essential: true, + portMappings: [{ + containerPort: 80, + targetGroup: loadbalancer.defaultTargetGroup, + }], + }, + }, +}); + +// The URL at which the container's HTTP endpoint will be available. +export const url = pulumi.interpolate`http://${loadbalancer.loadBalancer.dnsName}`; diff --git a/aws-ts-containers-dockerbuild/package.json b/aws-ts-containers-dockerbuild/package.json new file mode 100644 index 000000000..6080e6724 --- /dev/null +++ b/aws-ts-containers-dockerbuild/package.json @@ -0,0 +1,13 @@ +{ + "name": "aws-ts-containers-dockerbuildcloud", + "main": "index.ts", + "dependencies": { + "@pulumi/aws": "^6.44.0", + "@pulumi/awsx": "^2.13.0", + "@pulumi/docker-build": "^0.0.3", + "@pulumi/pulumi": "^3.124.0" + }, + "devDependencies": { + "@types/node": "^20.14.10" + } +} diff --git a/aws-ts-containers-dockerbuildcloud/README.md b/aws-ts-containers-dockerbuildcloud/README.md index 82478dc5f..a8eac4c5f 100644 --- a/aws-ts-containers-dockerbuildcloud/README.md +++ b/aws-ts-containers-dockerbuildcloud/README.md @@ -19,8 +19,8 @@ Last revision: May 2024. This Pulumi example is written as a template. It is meant to be copied via `pulumi new` as follows: ```bash -$ pulumi new https://github.com/pulumi/examples/tree/master/aws-ts-containers-dbc -$ npm install +pulumi new https://github.com/pulumi/examples/tree/master/aws-ts-containers-dockerbuildcloud +npm install ``` Once copied to your machine, feel free to edit as needed.