|
| 1 | +[](https://app.pulumi.com/new) |
| 2 | + |
| 3 | +# Get Started with Docker on AWS Fargate |
| 4 | + |
| 5 | +This example, inspired by the [Docker Getting Started Tutorial](https://docs.docker.com/get-started/), builds, deploys, |
| 6 | +and runs a simple containerized application to a private container registry, and scales out five load balanced replicas, |
| 7 | +all in just a handful of lines of Node.js code, and leveraging modern and best-in-class AWS features. |
| 8 | + |
| 9 | +To do this, we use Pulumi infrastructure as code to provision an |
| 10 | +[Elastic Container Service (ECS)](https://aws.amazon.com/ecs/) cluster, build our `Dockerfile` and deploy the |
| 11 | +resulting image to a private [Elastic Container Registry (ECR)](https://aws.amazon.com/ecr/) repository, and then create |
| 12 | +a scaled-out [Fargate](https://aws.amazon.com/fargate/) service behind an |
| 13 | +[Elastic Application Load Balancer](https://aws.amazon.com/elasticloadbalancing/) that allows traffic from the Internet |
| 14 | +on port 80. Because this example using AWS services directly, you can mix in other resources, like S3 buckets, RDS |
| 15 | +databases, and so on. |
| 16 | + |
| 17 | +# Prerequisites |
| 18 | + |
| 19 | +- [Node.js](https://nodejs.org/en/download/) |
| 20 | +- [Download and install the Pulumi CLI](https://pulumi.io/install) |
| 21 | +- [Connect Pulumi with your AWS account](https://pulumi.io/quickstart/aws/setup.html) (if your AWS CLI is |
| 22 | + configured, this will just work) |
| 23 | + |
| 24 | +# Running the Example |
| 25 | + |
| 26 | +After cloning this repo, `cd` into it and run these commands: |
| 27 | + |
| 28 | +1. Create a new stack, which is an isolated deployment target for this example: |
| 29 | + |
| 30 | + ```bash |
| 31 | + $ pulumi stack init dev |
| 32 | + ``` |
| 33 | + |
| 34 | +2. Set your desired AWS region: |
| 35 | + |
| 36 | + ```bash |
| 37 | + $ pulumi config set aws:region us-east-1 # any valid AWS region will work |
| 38 | + ``` |
| 39 | + |
| 40 | +3. Deploy everything with a single `pulumi up` command. This will show you a preview of changes first, which |
| 41 | + includes all of the required AWS resources (clusters, services, and the like). Don't worry if it's more than |
| 42 | + you expected -- this is one of the benefits of Pulumi, it configures everything so that so you don't need to! |
| 43 | +
|
| 44 | + ```bash |
| 45 | + $ pulumi up |
| 46 | + ``` |
| 47 | +
|
| 48 | + After being prompted and selecting "yes", your deployment will begin. It'll complete in a few minutes: |
| 49 | + |
| 50 | + ``` |
| 51 | + Updating (dev): |
| 52 | + |
| 53 | + Type Name Status |
| 54 | + + pulumi:pulumi:Stack aws-ts-hello-fargate-dev created |
| 55 | + + ├─ awsx:x:ecs:Cluster cluster created |
| 56 | + + │ ├─ awsx:x:ec2:SecurityGroup cluster created |
| 57 | + + │ │ ├─ awsx:x:ec2:EgressSecurityGroupRule cluster-egress created |
| 58 | + + │ │ │ └─ aws:ec2:SecurityGroupRule cluster-egress created |
| 59 | + + │ │ ├─ awsx:x:ec2:IngressSecurityGroupRule cluster-ssh created |
| 60 | + + │ │ │ └─ aws:ec2:SecurityGroupRule cluster-ssh created |
| 61 | + + │ │ ├─ awsx:x:ec2:IngressSecurityGroupRule cluster-containers created |
| 62 | + + │ │ │ └─ aws:ec2:SecurityGroupRule cluster-containers created |
| 63 | + + │ │ └─ aws:ec2:SecurityGroup cluster created |
| 64 | + + │ └─ aws:ecs:Cluster cluster created |
| 65 | + + ├─ awsx:x:elasticloadbalancingv2:ApplicationLoadBalancer net-lb created |
| 66 | + + │ ├─ awsx:x:elasticloadbalancingv2:ApplicationTargetGroup web created |
| 67 | + + │ │ └─ aws:elasticloadbalancingv2:TargetGroup ca84d134 created |
| 68 | + + │ ├─ awsx:x:elasticloadbalancingv2:ApplicationListener web created |
| 69 | + + │ │ ├─ awsx:x:ec2:IngressSecurityGroupRule web-external-0-ingress created |
| 70 | + + │ │ │ └─ aws:ec2:SecurityGroupRule web-external-0-ingress created |
| 71 | + + │ │ └─ aws:elasticloadbalancingv2:Listener web created |
| 72 | + + │ └─ aws:elasticloadbalancingv2:LoadBalancer 218ffe37 created |
| 73 | + + ├─ awsx:x:ec2:Vpc default-vpc created |
| 74 | + + │ ├─ awsx:x:ec2:Subnet default-vpc-public-0 created |
| 75 | + + │ ├─ awsx:x:ec2:Subnet default-vpc-public-1 created |
| 76 | + > │ ├─ aws:ec2:Subnet default-vpc-public-0 read |
| 77 | + > │ └─ aws:ec2:Subnet default-vpc-public-1 read |
| 78 | + + ├─ awsx:x:ecs:FargateTaskDefinition app-svc created |
| 79 | + + │ ├─ aws:ecr:Repository app-img created |
| 80 | + + │ ├─ aws:cloudwatch:LogGroup app-svc created |
| 81 | + + │ ├─ aws:iam:Role app-svc-task created |
| 82 | + + │ ├─ aws:iam:Role app-svc-execution created |
| 83 | + + │ ├─ aws:ecr:LifecyclePolicy app-img created |
| 84 | + + │ ├─ aws:iam:RolePolicyAttachment app-svc-task-32be53a2 created |
| 85 | + + │ ├─ aws:iam:RolePolicyAttachment app-svc-task-fd1a00e5 created |
| 86 | + + │ ├─ aws:iam:RolePolicyAttachment app-svc-execution-9a42f520 created |
| 87 | + + │ └─ aws:ecs:TaskDefinition app-svc created |
| 88 | + + ├─ awsx:x:ecs:FargateService app-svc created |
| 89 | + + │ └─ aws:ecs:Service app-svc created |
| 90 | + > └─ aws:ec2:Vpc default-vpc read |
| 91 | + |
| 92 | + Outputs: |
| 93 | + url: "218ffe37-e8023b7-1429118690.us-east-1.elb.amazonaws.com" |
| 94 | + |
| 95 | + Resources: |
| 96 | + + 34 created |
| 97 | + |
| 98 | + Duration: 3m30s |
| 99 | + |
| 100 | + Permalink: https://app.pulumi.com/acmecorp/aws-ts-hello-fargate/dev/updates/1 |
| 101 | + ``` |
| 102 | + |
| 103 | +4. At this point, your app is running! The URL was published so it's easy to interact with: |
| 104 | +
|
| 105 | + ```bash |
| 106 | + $ curl http://$(pulumi stack output url) |
| 107 | + <h3>Hello World!</h3> |
| 108 | + <b>Hostname:</b> ip-172-31-39-18.ec2.internal<br/> |
| 109 | + <b>Visits:</b> <i>cannot connect to Redis, counter disabled</i> |
| 110 | + ``` |
| 111 | +
|
| 112 | + For more details on how to enable Redis or advanced options, please see the instructions in the |
| 113 | + [Docker Getting Started guide](https://docs.docker.com/get-started/part6/). |
| 114 | +
|
| 115 | +6. Once you are done, you can destroy all of the resources, and the stack: |
| 116 | +
|
| 117 | + ```bash |
| 118 | + $ pulumi destroy |
| 119 | + $ pulumi stack rm |
| 120 | + ``` |
0 commit comments