Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

AWS ECS with container instances and delete orchestration

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set aws-py-ecs-instances-autoapi
cd pulumi-examples/aws-py-ecs-instances-autoapi

This example demonstrates three use-cases:

  • AWS ECS using Container Instances (Python): A Python Pulumi program that stands up a custom AWS ECS cluster that uses instances instead of Fargate for the infrastructure.
  • Automation API orchestration: Destroying this stack without any sort of orchestration will fail due to this issue in the underlying provider: https://github.com/hashicorp/terraform-provider-aws/issues/4852. So, Automation API to the rescue. By orchestrating sizing of the autoscaling group to 0 before the destroy, the destroy is able to complete as expected.
  • Automation API cross-language support: Although the automation logic is written in TypeScript, the ECS cluster stack is written in Python.

This directory contains two Pulumi projects:

  • py-ecs-instance/ — a Python Pulumi program that deploys an ECS cluster using “container instances” instead of Fargate, along with an nginx “hello world” test container and related load balancer and networking. You can change into this directory and run pulumi up to deploy the stack just as you would with any Pulumi project.
  • automation/ — the Automation API code (index.ts) that handles deploying and, more importantly, orchestrating the deletion of the stack to avoid a dependency constraint.

Prerequisites#

  1. Install Pulumi
  2. Configure AWS credentials
  3. Install Python
  4. Install Node.js
  5. The AWS CLI, with appropriate credentials

Running the example#

The recommended workflow is to run the Automation API program, which orchestrates both the deployment and the deletion of the ECS cluster stack. Change into the automation directory and use yarn to run the Automation API code:

Terminal window
cd automation
yarn install
yarn start
yarn run v1.19.1
$ ./node_modules/ts-node/dist/bin.js index.ts
successfully initialized stack
setting up config
config set
refreshing stack...
Refreshing (dev)
...
refresh complete
updating stack...
Updating (dev)
...
update summary:
{
"same": 0,
"update": 16
}
website url: http://load-balancer-xxxxxxxxx.us-east-1.elb.amazonaws.com

Cleaning up#

To destroy the stack, run the Automation API program with an additional destroy argument:

Terminal window
yarn start destroy
yarn run v1.19.1
$ ./node_modules/ts-node/dist/bin.js index.ts destroy
successfully initialized stack
setting up config
config set
refreshing stack...
Refreshing (dev)
destroying stack ...
Destroying (dev)
...
@ Destroying ...
...
Resources:
- 16 deleted
The resources in the stack have been deleted, but the history and configuration associated with the stack are still maintained.
If you want to remove the stack completely, run 'pulumi stack rm dev'.
stack destroy complete

Related

The infrastructure as code platform for any cloud.