Container delivery to Amazon ECS with Pulumi Delivery, in Python
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-py-delivery-ecscd pulumi-examples/aws-py-delivery-ecsThis example uses Pulumi Delivery to take a container image from a git push to a running beta environment on AWS. Every push that touches this directory starts a pipeline run:
- The build stage runs the
imagestack. It creates an Amazon ECR repository, builds the app inapp/with Docker Build, tags the image with the commit and pushes it. - The beta stage runs the
betastack with the image’s digest reference, so it deploys exactly the image the build stage pushed. The stack runs the image as an ECS Fargate service behind an Application Load Balancer, using AWSX. A job then calls the load balancer URL until the app answers.
The example has three Pulumi projects, all in Python, including the pipeline itself:
pipeline/defines the Delivery pipeline: a GitHub source, abuildstage and abetastage.image/creates an ECR repository, then builds the app inapp/and pushes its image.beta/runs that image as an ECS Fargate service behind a load balancer and exports its URL.
Prerequisites#
- Install Pulumi, version 3.277.0 or later, for
pulumi delivery register. - Install Python, version 3.10 or later.
- A Pulumi Cloud organization with Pulumi Delivery enabled.
- A GitHub repository that holds this example, either a fork of pulumi/examples or a copy of this directory in a repository of your own. Delivery runs every program from a checkout of that repository.
- The Pulumi GitHub App installed, with access to that repository. In Pulumi Cloud, go to Settings > Version control, open the GitHub installation, and choose Configure repository access to add it on GitHub.
- An AWS account (Configure AWS credentials), and a Pulumi ESC environment that logs in to it with OIDC. The steps below call it
<org>/<project>/<aws-env>.
Setting up the pipeline#
Run the commands below in order, in one shell, starting in this example’s directory. They use <org> for your Pulumi organization.
-
Create the two stacks that the pipeline updates, and point each at your AWS environment and region. Delivery does not create stacks, so they must exist before the first run.
The project names in the three
Pulumi.yamlfiles and the stack references inpipeline/__main__.pyare fixed. A second deployment in the same Pulumi organization must change them first, or it fails with a 409 “already exists” error. Use your names in the commands below.Terminal window cd imagepulumi stack init <org>/aws-py-delivery-ecs-image/imagepulumi config env add <project>/<aws-env> --yespulumi config set aws:region us-west-2cd ../betapulumi stack init <org>/aws-py-delivery-ecs-beta/betapulumi config env add <project>/<aws-env> --yespulumi config set aws:region us-west-2pulumi config set aws-native:region us-west-2cd .. -
Commit the two stack files. Delivery runs every program from your repository, so each stack’s
Pulumi.<stack>.yamlmust be in git. In a fork ofpulumi/examples, these files are ignored, so add them with-f:Terminal window git add -f image/Pulumi.image.yaml beta/Pulumi.beta.yamlgit commit -m "Add stack files for the Delivery example"git push -
Create the pipeline stack, point it at your repository, and tell Delivery where the pipeline program lives, with
delivery:sourcein the stack config.pulumi installinstalls thepulumi-deliverypackage fromrequirements.txtinto the virtual environment. Then commit and push everything:Terminal window cd pipelinepulumi stack init <org>/aws-py-delivery-ecs-pipeline/pipelinepulumi installpulumi config set repository <owner>/<name>pulumi config set branch <branch>pulumi config set path <path>pulumi config set --path 'delivery:source.url' https://github.com/<owner>/<name>pulumi config set --path 'delivery:source.branch' <branch>pulumi config set --path 'delivery:source.directory' <path>/pipelinegit add -f Pulumi.pipeline.yamlgit commit -m "Point the Delivery pipeline at this repository"git pushcd ..repositoryis your repository, as<owner>/<name>, andbranchis the branch to watch.pathis this example’s directory within the repository, for exampleaws-py-delivery-ecs. The example must live in a subdirectory: the pipeline starts runs for pushes that match<path>/**. With the example at the repository root (pathof.), pushes update the pipeline but start no run. -
Register the pipeline:
Terminal window cd pipelinepulumi delivery registercd ..A successful registration ends with
Registered the pipeline of <stack>and a link to the pipeline. Its stages show as awaiting until the first run starts. Registration fails if the checkout has uncommitted changes or the commit is not on the branch indelivery:source. -
Let the pipeline open the ESC environment. Registration creates a service principal, named “Delivery aws-py-delivery-ecs-pipeline pipeline”, and gives it write access to the pipeline, image and beta stacks. The stacks run as that principal, so it also needs to open the environment they import. In Pulumi Cloud, open the environment, go to Settings, then Access, choose Add Principal, then Service Principal, pick the pipeline’s service principal and give it Environment Open.
Or from the command line:
Terminal window ./scripts/pulumi-env-access-grant <project>/<aws-env> --principal "Delivery aws-py-delivery-ecs-pipeline pipeline" --by-name --permission open -
Start the first run: push a change to a file in this example, for example to the response text in
app/app.py, or choose Start a run on the pipeline in Pulumi Cloud, under Delivery in your organization. Registration does not start a run.Or from the command line, with your organization in place of
<org>. It prints the number of the new run:Terminal window pulumi api DispatchDeliveryRun -F orgName=<org> -F projectName=aws-py-delivery-ecs-pipeline -F stackName=pipeline --body '{}' -
Watch the run in Pulumi Cloud. The
buildstage pushes the image. Thebetastage deploys it, then thecheck-urljob requests the load balancer URL until the app answers and records the URL and status as the job’s outputs. The run also records the URL as thebeta-urlartifact of the release. The first run can take several minutes, mostly for the load balancer and the first Fargate task. From now on, every push that changes files in this example starts a new run.
Cleaning up#
From this example’s directory, destroy the stacks in reverse order and remove them. Each step runs only if the one before it succeeded:
(cd pipeline && pulumi destroy && pulumi stack rm) && (cd beta && pulumi destroy && pulumi stack rm) && (cd image && pulumi destroy && pulumi stack rm)Destroying the image stack deletes the ECR repository and every image in it. Destroying the pipeline stack removes the pipeline’s service principal and its access to the environment.
If you created the AWS environment only for this example, remove it with pulumi env rm <project>/<aws-env>, along with any AWS OIDC resources you created for it.