Hello Readers! In this blog we will see how we can create AWS ECS using pulumi. Pulumi is an open source infrastructure as code tool. We can create, deploy, and manage cloud infrastructure. Pulumi can be use with multi-programming languages, here I will use python.
Run the following command for installing pulumi:
$ curl -fsSL https://get.pulumi.com | sh
Check the version using following command:
$ pulumi version
Step1: After installing, configure pulumi to access your AWS account. If you have previously installed and configured the AWS CLI, then it’s okay. If not run the following command:
$ export AWS_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
$ export AWS_SECRET_ACCESS_KEY=<YOUR_SECRET_ACCESS_KEY>
Step2: Now that you have set up your environment by installing Pulumi and configuring with AWS, create a folder and inside this folder create a project. Use this command for creating the Pulumi project.
It will ask for entering the project name, its description , stack-name and aws-region in which the user wants to create. Enter these as per your wish. It will install all the dependencies and virtual environment and will create directories. Your new project is ready to go!
Step3: Activate your virtual environment and install its modules. Activate your virtual environment using the following command:
$ source venv/bin/activate
Installing its pulumi modules:
$ pip install pulumi_aws
$ pip install pulumi_awsx
Step4: The python code for creating AWS load balanced ECS is in main.py. Paste the following code in main.py and change cluster, load balancer,service name, image as per your need.
import pulumi import pulumi_aws as aws import pulumi_awsx as awsx cluster = aws.ecs.Cluster("my-ecs") lb = awsx.lb.ApplicationLoadBalancer("nginx-lb") service = awsx.ecs.FargateService("my-service", cluster=cluster.arn, desired_count=2, task_definition_args=awsx.ecs.FargateServiceTaskDefinitionArgs( container=awsx.ecs.TaskDefinitionContainerDefinitionArgs( image="nginx:latest", cpu=512, memory=128, essential=True, port_mappings=[awsx.ecs.TaskDefinitionPortMappingArgs( target_group=lb.default_target_group )], ) ) )
To run a Docker container in ECS using default network and cluster settings, we will use the awsx.ecs.FargateService class. I am using nginx image here. You can change it as you wish. It will create an ECS cluster,load balancer, security group, and services. We will access this container using a stable address, we will use a load balancer here.
Step5: Now we are all set for running the pulumi. Run the pulumi using the following command:
$ pulumi up
It will ask you Do you want to perform this update? So, Enter yes.
It’s going to create all the resources listed above.
It will take some time to deploy all the resources.
Step6: All the resources are created now. You can see below my ecs cluster is created successfully.
Lastly, let’s also verify by hitting the external DNS address of the ELB. You can find the DNS address in the EC2 Console under Load Balancing / Load Balancers.
Use the following command for deleting all the resources that are deployed now by pulumi:
$ pulumi destroy
It will again take some time to destroy all.
We are successfully done now!
Thank you for sticking to the end. In this blog we can create AWS load balanced ECS service using pulumi. . If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.