ECS Anywhere
A minimal AWS TypeScript Pulumi program
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set aws-ts-ecs-anywherecd pulumi-examples/aws-ts-ecs-anywhereThis example from our ECS Anywhere launch blog post shows how to deploy an ECS cluster along with a dockerized app to DigitalOcean.
To do this, we use Pulumi infrastructure as code to provision an
Elastic Container Service (ECS) cluster, build our Dockerfile and deploy the
resulting image to a private Elastic Container Registry (ECR) repository, and then create
a set of DigitalOcean droplets behind a load balancer to allow for zero downtime updates.
Prerequisites#
- Install Pulumi
- Configure AWS credentials (if your AWS CLI is configured, no further changes are required)
- Configure DigitalOcean credentials
- Install Node.js
Deploying the example#
After cloning this repo, cd into it and run these commands:
-
Create a new stack, which is an isolated deployment target for this example:
Terminal window pulumi stack init dev -
Set your desired AWS region:
Terminal window pulumi config set aws:region us-east-1 -
Install dependencies:
Terminal window npm install -
Deploy everything with a single
pulumi upcommand. This will show you a preview of changes first, which includes all of the required AWS resources (clusters, services, and the like). Don’t worry if it’s more than you expected — this is one of the benefits of Pulumi, it configures everything so that so you don’t need to!Terminal window pulumi upAfter being prompted and selecting “yes”, your deployment will begin. It’ll complete in a few minutes:
Updating (dev)View Live: https://app.pulumi.com/acmecorp/ecs-anywhere/dev/updates/1Type Name Status Info+ pulumi:pulumi:Stack ecs-anywhere-dev created+ ├─ awsx:ecr:Repository app created 1 warning+ │ ├─ aws:ecr:Repository app created+ │ └─ aws:ecr:LifecyclePolicy app created+ ├─ aws:ecs:Cluster cluster created+ ├─ aws:cloudwatch:LogGroup logGroup created+ ├─ digitalocean:index:Tag lb created+ ├─ aws:iam:Role taskRole created+ ├─ aws:iam:Role taskExecutionRole created+ ├─ aws:iam:Role ssmRole created+ ├─ digitalocean:index:LoadBalancer lb created+ ├─ aws:iam:RolePolicy taskRolePolicy created+ ├─ aws:iam:RolePolicyAttachment rpa-ecsanywhere-ecstaskexecution created+ ├─ aws:ssm:Activation ecsanywhere-ssmactivation created+ ├─ aws:iam:RolePolicyAttachment rpa-ssmrole-ec2containerservice created+ ├─ aws:iam:RolePolicyAttachment rpa-ssmrole-ssminstancecore created+ ├─ digitalocean:index:Droplet droplet-2 created+ ├─ digitalocean:index:Droplet droplet-1 created+ ├─ aws:ecs:TaskDefinition taskdefinition created+ └─ aws:ecs:Service service createdOutputs:clusterName: "cluster-de98e7f"ip : "165.227.252.130"Resources:+ 20 createdDuration: 1m30s -
At this point, your app is running! The URL was published so it’s easy to interact with:
Terminal window curl http://$(pulumi stack output ip)Hello World from Pulumi
Cleaning up#
Once you are done, there is an additional step before running the usual pulumi destroy. This is because the nodes are registered to AWS Systems Manager and the ECS cluster as part of the node setup and happen outside of the Pulumi stack. Run the following in your command line (you’ll need to install jq for this to work):
aws ssm describe-instance-information | jq ".InstanceInformationList | .[] | .InstanceId" | grep "mi-" | xargs -L 1 aws ssm deregister-managed-instance --instance-idaws ecs list-container-instances --cluster $(pulumi stack output clusterName) | jq ".containerInstanceArns | .[]" | xargs -L 1 aws ecs deregister-container-instance --cluster $(pulumi stack output clusterName) --force --container-instancepulumi refresh -ypulumi destroypulumi stack rm