How do I deploy the flexibleengine/mysql docker image on AWS EC2 with TypeScript?
In this guide, we will deploy a MySQL Docker container on an AWS EC2 instance using Pulumi with TypeScript. We will create an EC2 instance, install Docker on it, and run the MySQL container.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create a new security group for port 3306
const securityGroup = new aws.ec2.SecurityGroup("mysql-secgrp", {
description: "Enable MySQL access",
ingress: [
{
protocol: "tcp",
fromPort: 3306,
toPort: 3306,
cidrBlocks: ["0.0.0.0/0"],
},
],
});
// Get the latest Amazon Linux 2 AMI
const ami = aws.ec2.getAmi({
mostRecent: true,
owners: ["amazon"],
filters: [
{
name: "name",
values: ["amzn2-ami-hvm-*-x86_64-gp2"],
},
],
});
// Create an EC2 instance
const instance = new aws.ec2.Instance("mysql-instance", {
instanceType: "t2.micro",
securityGroups: [securityGroup.name],
ami: ami.then(ami => ami.id),
userData: `#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pw -d -p 3306:3306 flexibleengine/mysql`,
});
// Export the instance's publicly accessible URL
export const publicIp = instance.publicIp;
export const publicHostName = instance.publicDns;
Key Points
- Security Group: We create a security group to allow traffic on port 3306, which is the default port for MySQL.
- AMI: We use the latest Amazon Linux 2 AMI for the EC2 instance.
- EC2 Instance: We create an EC2 instance and use user data to install Docker, start the Docker service, and run the MySQL container.
- Exports: We export the public IP and DNS of the EC2 instance for easy access.
Summary
In this guide, we created an AWS EC2 instance using Pulumi and TypeScript, installed Docker on it, and deployed a MySQL Docker container. We also set up a security group to allow access to the MySQL port and exported the instance’s public IP and DNS for access.
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.