This gist contains code for blog post Using OpenSSH on AWS Platform
Created
September 24, 2015 07:48
-
-
Save jarjuk/73b2219a69ba9e8d614b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'aws-ssh-resolver' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AWSTemplateFormatVersion": "2010-09-09", | |
"Description": "Example configuration for VPC", | |
"Parameters": { | |
"InstanceType": { | |
"Description": "EC2 reousrce instance type", | |
"Type": "String", | |
"Default": "t2.micro" | |
}, | |
"KeyName": { | |
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance", | |
"Type": "AWS::EC2::KeyPair::KeyName", | |
"Default": "demo-key" | |
}, | |
"SSHLocation": { | |
"Description": "The IP address range that can be used to SSH to the EC2 instances", | |
"Type": "String", | |
"Default": "0.0.0.0/0" | |
} | |
}, | |
"Mappings": { | |
"AWSInstanceType2Arch": { | |
"t2.micro": { | |
"Arch": "64" | |
} | |
}, | |
"AWSRegionArch2AMI": { | |
"ap-northeast-1": { | |
"64": "ami-90815290" | |
}, | |
"ap-southeast-1": { | |
"64": "ami-0accf458" | |
}, | |
"ap-southeast-2": { | |
"64": "ami-1dc8b127" | |
}, | |
"cn-north-1": { | |
"64": "ami-eae27fd3" | |
}, | |
"eu-central-1": { | |
"64": "ami-3248712f" | |
}, | |
"eu-west-1": { | |
"64": "ami-d74437a0" | |
}, | |
"sa-east-1": { | |
"64": "ami-0f6ced12" | |
}, | |
"us-east-1": { | |
"64": "ami-83c525e8" | |
}, | |
"us-west-1": { | |
"64": "ami-61b25925" | |
}, | |
"us-gov-west-1": { | |
"64": "ami-51513172" | |
}, | |
"us-west-2": { | |
"64": "ami-57e8d767" | |
} | |
}, | |
"MappingNatAim": { | |
"us-east-1": { | |
"AMI": "ami-184dc970" | |
}, | |
"us-west-1": { | |
"AMI": "ami-a98396ec" | |
}, | |
"us-west-2": { | |
"AMI": "ami-290f4119" | |
}, | |
"eu-west-1": { | |
"AMI": "ami-14913f63" | |
}, | |
"eu-central-1": { | |
"AMI": "ami-ae380eb3" | |
}, | |
"sa-east-1": { | |
"AMI": "ami-8122969c" | |
}, | |
"ap-southeast-1": { | |
"AMI": "ami-6aa38238" | |
}, | |
"ap-southeast-2": { | |
"AMI": "ami-893f53b3" | |
}, | |
"ap-northeast-1": { | |
"AMI": "ami-27d6e626" | |
} | |
} | |
}, | |
"Resources": { | |
"MyVPC": { | |
"Type": "AWS::EC2::VPC", | |
"Properties": { | |
"CidrBlock": "10.0.0.0/16", | |
"EnableDnsSupport": true, | |
"EnableDnsHostnames": true, | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "MyVPC" | |
} | |
] | |
} | |
}, | |
"RouteTableNat": { | |
"Type": "AWS::EC2::RouteTable", | |
"Properties": { | |
"VpcId": { | |
"Ref": "MyVPC" | |
}, | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "RouteTableNat" | |
} | |
] | |
} | |
}, | |
"PublicSubnet": { | |
"Type": "AWS::EC2::Subnet", | |
"DependsOn": "MyVPC", | |
"Properties": { | |
"CidrBlock": "10.0.0.0/24", | |
"Tags": [ { "Key": "Name", "Value": "PublicSubnet" } ], | |
"MapPublicIpOnLaunch": true, | |
"VpcId": { "Ref": "MyVPC" } | |
} | |
}, | |
"PrivateSubnet": { | |
"Type": "AWS::EC2::Subnet", | |
"DependsOn": "MyVPC", | |
"Properties": { | |
"CidrBlock": "10.0.1.0/24", | |
"Tags": [ { "Key": "Name", "Value": "PrivateSubnet" } ], | |
"MapPublicIpOnLaunch": false, | |
"VpcId": { "Ref": "MyVPC" } | |
} | |
}, | |
"PrivateSubnetRouteTableAssociation": { | |
"Type": "AWS::EC2::SubnetRouteTableAssociation", | |
"Properties": { | |
"RouteTableId": { | |
"Ref": "RouteTableNat" | |
}, | |
"SubnetId": { | |
"Ref": "PrivateSubnet" | |
} | |
} | |
}, | |
"MyInternetGw": { | |
"Type": "AWS::EC2::InternetGateway", | |
"Properties": { | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "MyInternetGw" | |
} | |
] | |
} | |
}, | |
"MyInternetGwAttachment": { | |
"Type": "AWS::EC2::VPCGatewayAttachment", | |
"Properties": { | |
"VpcId": { | |
"Ref": "MyVPC" | |
}, | |
"InternetGatewayId": { | |
"Ref": "MyInternetGw" | |
} | |
} | |
}, | |
"RouteTableMyInternetGw": { | |
"Type": "AWS::EC2::RouteTable", | |
"Properties": { | |
"VpcId": { | |
"Ref": "MyVPC" | |
}, | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "RouteTableMyInternetGw" | |
}, | |
{ | |
"Key": "Application", | |
"Value": { | |
"Ref": "AWS::StackId" | |
} | |
} | |
] | |
} | |
}, | |
"Route": { | |
"Type": "AWS::EC2::Route", | |
"DependsOn": "MyInternetGwAttachment", | |
"Properties": { | |
"RouteTableId": { | |
"Ref": "RouteTableMyInternetGw" | |
}, | |
"DestinationCidrBlock": "0.0.0.0/0", | |
"GatewayId": { | |
"Ref": "MyInternetGw" | |
} | |
} | |
}, | |
"RouteTableAssociationPublicSubnet": { | |
"Type": "AWS::EC2::SubnetRouteTableAssociation", | |
"Properties": { | |
"SubnetId": { | |
"Ref": "PublicSubnet" | |
}, | |
"RouteTableId": { | |
"Ref": "RouteTableMyInternetGw" | |
} | |
} | |
}, | |
"BackendSecurityGroup": { | |
"Type": "AWS::EC2::SecurityGroup", | |
"Properties": { | |
"GroupDescription": "Enable SSH access via port 22", | |
"VpcId": { | |
"Ref": "MyVPC" | |
}, | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "BackendSecurityGroup" | |
} | |
], | |
"SecurityGroupEgress": [ | |
{ | |
"IpProtocol": "tcp", | |
"FromPort": "80", | |
"ToPort": "80", | |
"CidrIp": "0.0.0.0/0" | |
}, | |
{ | |
"IpProtocol": "icmp", | |
"FromPort": "-1", | |
"ToPort": "-1", | |
"CidrIp": "0.0.0.0/0" | |
} | |
], | |
"SecurityGroupIngress": [ | |
{ | |
"IpProtocol": "tcp", | |
"FromPort": "22", | |
"ToPort": "22", | |
"CidrIp": "10.0.0.0/24" | |
}, | |
{ | |
"IpProtocol": "icmp", | |
"FromPort": "-1", | |
"ToPort": "-1", | |
"CidrIp": "10.0.0.0/16" | |
} | |
] | |
} | |
}, | |
"FrontEndSecurityGroup": { | |
"Type": "AWS::EC2::SecurityGroup", | |
"Properties": { | |
"GroupDescription": "Enable SSH access via port 22", | |
"VpcId": { | |
"Ref": "MyVPC" | |
}, | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "FrontEndSecurityGroup" | |
} | |
], | |
"SecurityGroupEgress": [ | |
{ | |
"IpProtocol": "tcp", | |
"FromPort": "22", | |
"ToPort": "22", | |
"CidrIp": "10.0.1.0/24" | |
}, | |
{ | |
"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", | |
"CidrIp": "0.0.0.0/0" | |
} | |
], | |
"SecurityGroupIngress": [ | |
{ | |
"IpProtocol": "tcp", | |
"FromPort": "22", | |
"ToPort": "22", | |
"CidrIp": { | |
"Ref": "SSHLocation" | |
} | |
}, | |
{ | |
"IpProtocol": "icmp", | |
"FromPort": "-1", | |
"ToPort": "-1", | |
"CidrIp": "0.0.0.0/0" | |
}, | |
{ | |
"IpProtocol": "tcp", | |
"FromPort": "80", | |
"ToPort": "80", | |
"CidrIp": "0.0.0.0/0" | |
} | |
] | |
} | |
}, | |
"myBack1": { | |
"Type": "AWS::EC2::Instance", | |
"Metadata": {}, | |
"Properties": { | |
"ImageId": { | |
"Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" }, | |
{ "Fn::FindInMap": [ "AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch" ] } ] | |
}, | |
"InstanceType": { "Ref": "InstanceType" }, | |
"Tags": [ { "Key": "Name", "Value": "myBack1" } ], | |
"SourceDestCheck": true, | |
"SecurityGroupIds": [ { "Ref": "BackendSecurityGroup" } ], | |
"SubnetId": { "Ref": "PrivateSubnet" }, | |
"KeyName": { "Ref": "KeyName" }, | |
"UserData": { | |
"Fn::Base64": { | |
"Fn::Join": [ | |
"\n", | |
[ | |
"#!/bin/bash\n", | |
"set -x\n", | |
"set -e\n", | |
"set -o pipefail\n", | |
"LOG=/tmp/install.log\n", | |
"echo $(date): User data script started > $LOG\n", | |
"echo $(date): User data script started\n", | |
"function finish() {\n", | |
" echo \"$(date): installation finished\" \n", | |
" echo \"$(date): installation finished\" >> $LOG \n", | |
"}\n", | |
"function error() {\n", | |
" local lineno=$1\n", | |
" local error=1\n", | |
" echo \"$(date): installation finished in ERROR $error on line $lineno\" \n", | |
" echo \"$(date): installation finished in ERROR $error on line $lineno\" >> $LOG \n", | |
" exit 1\n", | |
"}\n", | |
"trap finish EXIT\n", | |
"trap 'error ${LINENO}' ERR\n" | |
] | |
] | |
} | |
} | |
} | |
}, | |
"myFront1": { | |
"Type": "AWS::EC2::Instance", | |
"Metadata": {}, | |
"Properties": { | |
"ImageId": { "Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" }, | |
{ "Fn::FindInMap": [ "AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch" ] } ] | |
}, | |
"InstanceType": { "Ref": "InstanceType" }, | |
"Tags": [ { "Key": "Name", "Value": "myFront1" } ], | |
"SourceDestCheck": true, | |
"SecurityGroupIds": [ { "Ref": "FrontEndSecurityGroup" } ], | |
"SubnetId": { "Ref": "PublicSubnet" }, | |
"KeyName": { "Ref": "KeyName" }, | |
"UserData": { | |
"Fn::Base64": { | |
"Fn::Join": [ | |
"\n", | |
[ | |
"#!/bin/bash\n", | |
"set -x\n", | |
"set -e\n", | |
"set -o pipefail\n", | |
"LOG=/tmp/install.log\n", | |
"echo $(date): User data script started > $LOG\n", | |
"echo $(date): User data script started\n", | |
"function finish() {\n", | |
" echo \"$(date): installation finished\" \n", | |
" echo \"$(date): installation finished\" >> $LOG \n", | |
"}\n", | |
"function error() {\n", | |
" local lineno=$1\n", | |
" local error=1\n", | |
" echo \"$(date): installation finished in ERROR $error on line $lineno\" \n", | |
" echo \"$(date): installation finished in ERROR $error on line $lineno\" >> $LOG \n", | |
" exit 1\n", | |
"}\n", | |
"trap finish EXIT\n", | |
"trap 'error ${LINENO}' ERR\n" | |
] | |
] | |
} | |
} | |
} | |
} | |
}, | |
"Outputs": { | |
"InstanceId1": { | |
"Description": "Id of the newly created EC2 instance", | |
"Value": { | |
"Ref": "myFront1" | |
} | |
}, | |
"InstanceId2": { | |
"Description": "Id of the newly created EC2 instance", | |
"Value": { | |
"Ref": "myBack1" | |
} | |
}, | |
"MyInternetGw": { | |
"Description": "Id of InternetGateway", | |
"Value": { | |
"Ref": "MyInternetGw" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment