How do I deploy the LocalStack docker image on AWS EC2 with TypeScript?
In this example, we will deploy the LocalStack Docker image on an AWS EC2 instance using Pulumi with TypeScript. LocalStack is a fully functional local AWS cloud stack that allows you to develop and test your cloud applications offline.
Key Points:
- We will create an EC2 instance.
- We will use a user data script to install Docker and run the LocalStack container.
- We will configure the security group to allow necessary traffic.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create a new security group for the EC2 instance
const securityGroup = new aws.ec2.SecurityGroup("localstack-sg", {
description: "Allow HTTP and SSH traffic",
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, // SSH
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }, // HTTP
],
egress: [
{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
],
});
// Define the user data script to install Docker and run LocalStack
const userData = `#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker run -d -p 80:8080 localstack/localstack`;
// Create an EC2 instance
const instance = new aws.ec2.Instance("localstack-instance", {
instanceType: "t2.micro",
ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
userData: userData,
vpcSecurityGroupIds: [securityGroup.id],
tags: {
Name: "localstack-instance",
},
});
// Export the public IP of the instance
export const publicIp = instance.publicIp;
Summary
In this example, we created an AWS EC2 instance and installed Docker on it using a user data script. We then ran the LocalStack Docker container on the EC2 instance. We also configured a security group to allow SSH and HTTP traffic. Finally, we exported the public IP of the instance. This setup allows you to run LocalStack on an EC2 instance for local AWS cloud stack development and testing.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.