How do I create a MySQL database on AWS?
In this guide, we’ll walk through creating a MySQL database on AWS. We’ll see how to set up the necessary resources such as the database instance, subnet groups, security groups, and parameter groups. This setup will ensure your database runs securely and efficiently within your defined parameters.
Detailed Steps:
- Provider Configuration: Define the AWS provider to allow Terraform to interact with AWS resources.
- VPC and Subnets: Set up the VPC and subnets where the database will reside.
- Security Group: Configure the security group to control access to the database instance.
- Subnet Group: Define a DB subnet group to specify which subnets the RDS instance will use.
- Parameter Group: Create an RDS parameter group to customize database options.
- RDS Instance: Create the actual MySQL database instance.
Here’s how the HCL code for this example looks:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// VPC and Subnets
const main = new aws.ec2.Vpc("main", {
cidrBlock: "10.0.0.0/16",
tags: {
Name: "main-vpc",
},
});
const main1 = new aws.ec2.Subnet("main1", {
vpcId: main.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
tags: {
Name: "main-subnet-1",
},
});
const main2 = new aws.ec2.Subnet("main2", {
vpcId: main.id,
cidrBlock: "10.0.2.0/24",
availabilityZone: "us-west-2b",
tags: {
Name: "main-subnet-2",
},
});
// Security Group
const mainSecurityGroup = new aws.ec2.SecurityGroup("main", {
vpcId: main.id,
ingress: [{
fromPort: 3306,
toPort: 3306,
protocol: "tcp",
cidrBlocks: ["0.0.0.0/0"],
}],
egress: [{
fromPort: 0,
toPort: 0,
protocol: "-1",
cidrBlocks: ["0.0.0.0/0"],
}],
tags: {
Name: "main-sec-group",
},
});
// DB Subnet Group
const mainSubnetGroup = new aws.rds.SubnetGroup("main", {
name: "main-subnet-group",
subnetIds: [
main1.id,
main2.id,
],
tags: {
Name: "main-subnet-group",
},
});
// Parameter Group
const mainParameterGroup = new aws.rds.ParameterGroup("main", {
name: "main-parameter-group",
family: "mysql5.7",
parameters: [{
name: "character_set_server",
value: "utf8mb4",
}],
tags: {
Name: "main-parameter-group",
},
});
// RDS Instance
const mainInstance = new aws.rds.Instance("main", {
engine: "mysql",
instanceClass: aws.rds.InstanceType.T2_Micro,
allocatedStorage: 20,
name: "mydatabase",
username: "admin",
password: "password",
dbSubnetGroupName: mainSubnetGroup.name,
parameterGroupName: mainParameterGroup.name,
vpcSecurityGroupIds: [mainSecurityGroup.id],
skipFinalSnapshot: true,
tags: {
Name: "main-rds-instance",
},
});
export const dbInstanceEndpoint = mainInstance.endpoint;
export const dbInstanceIdentifier = mainInstance.identifier;
Conclusion
This code sets up an AWS MySQL RDS instance within a VPC, associating it with necessary subnets, security groups, and parameter groups. It also outputs the database endpoint and identifier to access them easily. This configuration ensures that your database is secure, well-defined, and ready to handle your application needs.
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.