Skip to content

Instantly share code, notes, and snippets.

@kangks
Last active January 17, 2019 09:25
Show Gist options
  • Save kangks/1e5995c73779abedbf311c6c2de6baab to your computer and use it in GitHub Desktop.
Save kangks/1e5995c73779abedbf311c6c2de6baab to your computer and use it in GitHub Desktop.
AWS Cloudformation for Ec2 with Postgres 96
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Create Linux EC2 with postgresql96",
"Metadata": {
"Comment": "Postgres 9.6 on a r3.8xlarge + 20G EBS in a privateSubnet, and pgbench in publicSubnet"
},
"Parameters": {
"Ec2KeyNameParam": {
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type": "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription": "must be the name of an existing EC2 KeyPair."
},
"Ec2Az": {
"Description": "EC2 Availability Zone",
"Type": "AWS::EC2::AvailabilityZone::Name",
"ConstraintDescription": "AZ for Ec2"
},
"DBInstanceType": {
"Description": "Postgres EC2 instance type",
"Type": "String",
"Default": "r3.8xlarge",
"AllowedValues": [
"t1.micro",
"t2.micro",
"t2.small",
"t2.medium",
"m1.small",
"m1.medium",
"m1.large",
"m1.xlarge",
"m2.xlarge",
"m2.2xlarge",
"m2.4xlarge",
"m3.medium",
"m3.large",
"m3.xlarge",
"m3.2xlarge",
"c1.medium",
"c1.xlarge",
"c3.large",
"c3.xlarge",
"c3.2xlarge",
"c3.4xlarge",
"c3.8xlarge",
"g2.2xlarge",
"r3.large",
"r3.xlarge",
"r3.2xlarge",
"r3.4xlarge",
"r3.8xlarge",
"i2.xlarge",
"i2.2xlarge",
"i2.4xlarge",
"i2.8xlarge",
"hi1.4xlarge",
"hs1.8xlarge",
"cr1.8xlarge",
"cc2.8xlarge",
"cg1.4xlarge"
],
"ConstraintDescription": "Must be a valid EC2 instance type"
},
"BenchmarckInstanceType": {
"Description": "Postbench EC2 instance type",
"Type": "String",
"Default": "t2.micro",
"AllowedValues": [
"t1.micro",
"t2.micro",
"t2.small",
"t2.medium",
"m1.small",
"m1.medium",
"m1.large",
"m1.xlarge",
"m2.xlarge",
"m2.2xlarge",
"m2.4xlarge",
"m3.medium",
"m3.large",
"m3.xlarge",
"m3.2xlarge",
"c1.medium",
"c1.xlarge",
"c3.large",
"c3.xlarge",
"c3.2xlarge",
"c3.4xlarge",
"c3.8xlarge",
"g2.2xlarge",
"r3.large",
"r3.xlarge",
"r3.2xlarge",
"r3.4xlarge",
"r3.8xlarge",
"i2.xlarge",
"i2.2xlarge",
"i2.4xlarge",
"i2.8xlarge",
"hi1.4xlarge",
"hs1.8xlarge",
"cr1.8xlarge",
"cc2.8xlarge",
"cg1.4xlarge"
],
"ConstraintDescription": "Must be a valid EC2 instance type"
}
},
"Mappings": {
"AWSInstanceType2Arch": {
"t1.micro": {
"Arch": "PV64"
},
"t2.micro": {
"Arch": "HVM64"
},
"t2.small": {
"Arch": "HVM64"
},
"t2.medium": {
"Arch": "HVM64"
},
"m1.small": {
"Arch": "PV64"
},
"m1.medium": {
"Arch": "PV64"
},
"m1.large": {
"Arch": "PV64"
},
"m1.xlarge": {
"Arch": "PV64"
},
"m2.xlarge": {
"Arch": "PV64"
},
"m2.2xlarge": {
"Arch": "PV64"
},
"m2.4xlarge": {
"Arch": "PV64"
},
"m3.medium": {
"Arch": "HVM64"
},
"m3.large": {
"Arch": "HVM64"
},
"m3.xlarge": {
"Arch": "HVM64"
},
"m3.2xlarge": {
"Arch": "HVM64"
},
"c1.medium": {
"Arch": "PV64"
},
"c1.xlarge": {
"Arch": "PV64"
},
"c3.large": {
"Arch": "HVM64"
},
"c3.xlarge": {
"Arch": "HVM64"
},
"c3.2xlarge": {
"Arch": "HVM64"
},
"c3.4xlarge": {
"Arch": "HVM64"
},
"c3.8xlarge": {
"Arch": "HVM64"
},
"g2.2xlarge": {
"Arch": "HVMG2"
},
"r3.large": {
"Arch": "HVM64"
},
"r3.xlarge": {
"Arch": "HVM64"
},
"r3.2xlarge": {
"Arch": "HVM64"
},
"r3.4xlarge": {
"Arch": "HVM64"
},
"r3.8xlarge": {
"Arch": "HVM64"
},
"i2.xlarge": {
"Arch": "HVM64"
},
"i2.2xlarge": {
"Arch": "HVM64"
},
"i2.4xlarge": {
"Arch": "HVM64"
},
"i2.8xlarge": {
"Arch": "HVM64"
},
"hi1.4xlarge": {
"Arch": "HVM64"
},
"hs1.8xlarge": {
"Arch": "HVM64"
},
"cr1.8xlarge": {
"Arch": "HVM64"
},
"cc2.8xlarge": {
"Arch": "HVM64"
}
},
"AWSRegionArch2AMI": {
"us-east-1": {
"PV64": "ami-0b4cfd60",
"HVM64": "ami-2ccae744"
},
"us-west-2": {
"PV64": "",
"HVM64": ""
},
"us-west-1": {
"PV64": "",
"HVM64": ""
},
"eu-west-1": {
"PV64": "",
"HVM64": ""
},
"ap-southeast-1": {
"PV64": "",
"HVM64": ""
},
"ap-northeast-1": {
"PV64": "",
"HVM64": ""
},
"ap-southeast-2": {
"PV64": "",
"HVM64": ""
},
"sa-east-1": {
"PV64": "",
"HVM64": ""
},
"cn-north-1": {
"PV64": "",
"HVM64": ""
},
"eu-central-1": {
"PV64": "",
"HVM64": ""
}
}
},
"Outputs": {},
"Resources": {
"PostgresVPC" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.10.0.0/16",
"Tags" : [
{
"Key" : "Name",
"Value" :
{ "Fn::Join":
[
"-",
[
{"Ref": "AWS::StackName" },
"vpc"
]
]
}
}
]
}
},
"SGSSHPostgresSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Security group for ingress SSH and Postgres traffic",
"SecurityGroupIngress": [
{
"CidrIp": "0.0.0.0/0",
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22"
},
{
"CidrIp": "0.0.0.0/0",
"IpProtocol": "tcp",
"FromPort": "5432",
"ToPort": "5432"
},
{
"CidrIp": "10.10.0.0/16",
"IpProtocol": "-1",
"FromPort": "0",
"ToPort": "65535"
}
],
"SecurityGroupEgress": [
{
"CidrIp": "10.10.0.0/16",
"IpProtocol": "-1",
"FromPort": "0",
"ToPort": "65535"
},
{
"CidrIp": "0.0.0.0/0",
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80"
},
{
"CidrIp": "0.0.0.0/0",
"IpProtocol": "tcp",
"FromPort": "443",
"ToPort": "443"
},
{
"CidrIp": "0.0.0.0/0",
"IpProtocol": "icmp",
"FromPort": "-1",
"ToPort": "-1"
}
],
"VpcId": { "Ref": "PostgresVPC"}
}
},
"publicSubnet" : {
"Type" : "AWS::EC2::Subnet",
"Properties" : {
"VpcId" : { "Ref" : "PostgresVPC" },
"CidrBlock" : "10.10.1.0/24",
"AvailabilityZone" : {
"Ref": "Ec2Az"
},
"Tags" : [
{
"Key" : "Name",
"Value" :
{ "Fn::Join":
[
"-",
[
{"Ref": "AWS::StackName" },
"public"
]
]
}
}
]
}
},
"privateSubnet" : {
"Type" : "AWS::EC2::Subnet",
"Properties" : {
"VpcId" : { "Ref" : "PostgresVPC" },
"CidrBlock" : "10.10.2.0/24",
"AvailabilityZone" : {
"Ref": "Ec2Az"
},
"Tags" : [
{
"Key" : "Name",
"Value" :
{ "Fn::Join":
[
"-",
[
{"Ref": "AWS::StackName" },
"private"
]
]
}
}
]
}
},
"PostgresEc2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"AWSRegionArch2AMI",
{
"Ref": "AWS::Region"
},
{
"Fn::FindInMap": [
"AWSInstanceType2Arch",
{
"Ref": "DBInstanceType"
},
"Arch"
]
}
]
},
"InstanceType": {
"Ref": "DBInstanceType"
},
"KeyName": {
"Ref": "Ec2KeyNameParam"
},
"AvailabilityZone": {
"Ref": "Ec2Az"
},
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sdm",
"Ebs": {
"VolumeType": "io1",
"Iops": "200",
"DeleteOnTermination": "false",
"VolumeSize": "20"
}
}
],
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": "false",
"DeviceIndex": "0",
"GroupSet": [
{
"Ref": "SGSSHPostgresSecurityGroup"
}
],
"SubnetId": {
"Ref": "privateSubnet"
}
}
],
"Tags" : [
{
"Key" : "Name",
"Value" :
{ "Fn::Join":
[
"-",
[
"postgres",
{"Ref": "AWS::StackName" }
]
]
}
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\n",
"mkfs -t ext4 /dev/xvdm\n",
"mkdir /opt/mount1\n",
"echo \"/dev/xvdm /opt/mount1 ext4 defaults,nofail 0 2\" >> /etc/fstab \n",
"mount -a\n",
"rpm -i https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-ami201503-96-9.6-2.noarch.rpm && ",
"yum update -y && ",
"yum install -y postgresql96 postgresql96-server postgresql96-libs postgresql96-contrib openssh-clients git\n",
"mkdir -p /opt/mount1/pg96 && chown postgres:postgres /opt/mount1/pg96 &&",
"echo \"PGDATA=/opt/mount1/pg96\" > /etc/sysconfig/pgsql/postgresql-9.6\n",
"/etc/init.d/postgresql-9.6 initdb && ",
"cp -p /opt/mount1/pg96/postgresql.conf /opt/mount1/pg96/postgresql.conf.bak && ",
"echo \"listen_addresses '*'\" >> /opt/mount1/pg96/postgresql.conf;\n",
"echo \"host all all 10.10.0.0/16 trust\" >> /opt/mount1/pg96/pg_hba.conf;\n",
"/etc/init.d/postgresql-9.6 start && ",
"chkconfig postgresql-9.6 on && ",
"sudo -u postgres psql -c 'SELECT version();'"
]
]
}
}
}
},
"PostbenchEc2": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"AWSRegionArch2AMI",
{
"Ref": "AWS::Region"
},
{
"Fn::FindInMap": [
"AWSInstanceType2Arch",
{
"Ref": "BenchmarckInstanceType"
},
"Arch"
]
}
]
},
"InstanceType": {
"Ref": "BenchmarckInstanceType"
},
"KeyName": {
"Ref": "Ec2KeyNameParam"
},
"AvailabilityZone":{
"Ref": "Ec2Az"
},
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sdm",
"Ebs": {
"VolumeType": "io1",
"Iops": "200",
"DeleteOnTermination": "false",
"VolumeSize": "20"
}
}
],
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": "false",
"DeviceIndex": "0",
"GroupSet": [
{
"Ref": "SGSSHPostgresSecurityGroup"
}
],
"SubnetId": {
"Ref": "publicSubnet"
}
}
],
"Tags" : [
{
"Key" : "Name",
"Value" :
{ "Fn::Join":
[
"-",
[
"pgbench",
{"Ref": "AWS::StackName" }
]
]
}
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\n",
"rpm -i https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-ami201503-96-9.6-2.noarch.rpm && ",
"yum update -y && ",
"yum install -y postgresql96 postgresql96-libs postgresql96-contrib openssh-clients git\n",
"echo '/usr/pgsql-9.6/bin/pgbench -h ${PGHOST} -U postgres -p 5432 -i -s 100 && /usr/pgsql-9.6/bin/pgbench -U postgres -c16 -j8 -T60 -h ${PGHOST} -p 5432' > /tmp/pgbench.bash;"
]
]
}
}
}
},
"publicEc2EIP": {
"Type": "AWS::EC2::EIP",
"DependsOn": [ "PostbenchEc2" ],
"Properties": {
"InstanceId": {
"Ref": "PostbenchEc2"
}
}
},
"postgresIGW" : {
"Type" : "AWS::EC2::InternetGateway",
"Properties" : {
"Tags" : [
{
"Key" : "Name",
"Value" : {"Ref": "AWS::StackName" }
}
]
}
},
"AttachInternetGateway" : {
"Type" : "AWS::EC2::VPCGatewayAttachment",
"Properties" : {
"VpcId" : { "Ref" : "PostgresVPC" },
"InternetGatewayId" : { "Ref" : "postgresIGW" }
}
},
"publicRouteTable" : {
"Type" : "AWS::EC2::RouteTable",
"Properties" : {
"VpcId" : { "Ref" : "PostgresVPC" },
"Tags" : [
{
"Key" : "Name",
"Value" :
{ "Fn::Join":
[
"-",
[
"public",
{"Ref": "AWS::StackName" }
]
]
}
}
]
}
},
"publicRouteTableAssociation" : {
"Type" : "AWS::EC2::SubnetRouteTableAssociation",
"Properties" : {
"SubnetId" : { "Ref" : "publicSubnet" },
"RouteTableId" : { "Ref" : "publicRouteTable" }
}
},
"publicIgwRoute" : {
"Type" : "AWS::EC2::Route",
"DependsOn" : "postgresIGW",
"Properties" : {
"RouteTableId" : { "Ref" : "publicRouteTable" },
"DestinationCidrBlock" : "0.0.0.0/0",
"GatewayId" : { "Ref" : "postgresIGW" }
}
},
"NAT" : {
"DependsOn" : "PostgresVPC",
"Type" : "AWS::EC2::NatGateway",
"Properties" : {
"AllocationId" : { "Fn::GetAtt" : ["NatEIP", "AllocationId"]},
"SubnetId" : { "Ref" : "publicSubnet"}
}
},
"NatEIP" : {
"Type" : "AWS::EC2::EIP",
"Properties" : {
"Domain" : "vpc"
}
},
"privateRouteTable" : {
"Type" : "AWS::EC2::RouteTable",
"Properties" : {
"VpcId" : { "Ref" : "PostgresVPC" },
"Tags" : [
{
"Key" : "Name",
"Value" :
{ "Fn::Join":
[
"-",
[
"private",
{"Ref": "AWS::StackName" }
]
]
}
}
]
}
},
"privateRoute" : {
"Type" : "AWS::EC2::Route",
"Properties" : {
"RouteTableId" : { "Ref" : "privateRouteTable" },
"DestinationCidrBlock" : "0.0.0.0/0",
"NatGatewayId" : { "Ref" : "NAT" }
}
},
"privateRouteTableAssociation" : {
"Type" : "AWS::EC2::SubnetRouteTableAssociation",
"Properties" : {
"SubnetId" : { "Ref" : "privateSubnet" },
"RouteTableId" : { "Ref" : "privateRouteTable" }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment