Use Pulumi Crosswalk to setup an ECS Service in a VPC

Nathan Peck profile picture
Nathan Peck
Senior Developer Advocate at AWS

Pulumi is an infrastructure as code framework for software engineers. Instead of writing YAML to define your infrastructure you can use higher level SDK commands, in a familiar programming language, and Pulumi will create the necessary resources for you automatically.

  • TypeScript
  • Python
  • Go
  • Java
  • YAML
  • C#
File: service-in-vpc.tsLanguage: ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

const vpc = new awsx.ec2.Vpc("vpc", {});
const securityGroup = new aws.ec2.SecurityGroup("securityGroup", {
  vpcId: vpc.vpcId,
  egress: [{
    fromPort: 0,
    toPort: 0,
    protocol: "-1",
    cidrBlocks: ["0.0.0.0/0"],
    ipv6CidrBlocks: ["::/0"],
  }],
});
const cluster = new aws.ecs.Cluster("cluster", {});
const service = new awsx.ecs.FargateService("service", {
  cluster: cluster.arn,
  networkConfiguration: {
    subnets: vpc.privateSubnetIds,
    securityGroups: [securityGroup.id],
  },
  desiredCount: 2,
  taskDefinitionArgs: {
    container: {
      image: "nginx:latest",
      cpu: 512,
      memory: 128,
      essential: true,
    },
  },
});

Setup

  1. Ensure that you have Pulumi setup on your system and configured to connect to AWS.
  2. Start a new Pulumi project
  3. Copy the code above into your Pulumi project

Usage

Show a preview of resources to be deployed:

Language: sh
pulumi preview

Deploy the resources to your AWS account:

Language: sh
pulumi up

Print out the URL of the deployed ECS service's load balancer:

Language: sh
pulumi stack output url

Tear down the stack and all of its resources:

Language: sh
pulumi destroy