Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Example] aws-ts-containers-dockerbuild #1668

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions aws-ts-containers-dockerbuild/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions aws-ts-containers-dockerbuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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
- Docker desktop with a default builder.

## 👩‍🏫 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
```
2 changes: 2 additions & 0 deletions aws-ts-containers-dockerbuild/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM nginx
COPY content /usr/share/nginx/html
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions aws-ts-containers-dockerbuild/app/content/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><meta charset="UTF-8">
<title>Hello, Pulumi!</title></head>
<body>
<p>Hello, containers!</p>
<p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a> and deployed to Fargate</p>
</body></html>
91 changes: 91 additions & 0 deletions aws-ts-containers-dockerbuild/index.ts
Original file line number Diff line number Diff line change
@@ -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.<STACK>.yaml file
// Configuration values can be set with the pulumi config set command
// OR by editing the Pulumi.<STACK>.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}`;
13 changes: 13 additions & 0 deletions aws-ts-containers-dockerbuild/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 2 additions & 2 deletions aws-ts-containers-dockerbuildcloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading