Skip to content

Instantly share code, notes, and snippets.

@kei-sato
Created September 12, 2016 09:19
Show Gist options
  • Save kei-sato/0d0b3b498a202bc8619f1d357c58f090 to your computer and use it in GitHub Desktop.
Save kei-sato/0d0b3b498a202bc8619f1d357c58f090 to your computer and use it in GitHub Desktop.
# : how to create key pair which is used to ssh login to ec2 instances
# keypair_name=mykey1
# ssh-keygen -t rsa -f ~/.ssh/${keypair_name}
# aws ec2 import-key-pair --key-name ${keypair_name} --public-key-material file://$HOME/.ssh/${keypair_name}.pub
# : modify these variables to fit your situation
# StackName=ec2-run-instance
# yaml2json > /tmp/parameters.json << 'EOF'
# - ParameterKey: KeyName
# ParameterValue: mykey1
# - ParameterKey: ImageId
# ParameterValue: ami-b36981d8
# - ParameterKey: InstanceType
# ParameterValue: g2.2xlarge
# - ParameterKey: AllowSSHFromCidr
# ParameterValue: 0.0.0.0/0
# EOF
# : convert yaml (this file) to json which requires yaml2json (brew install remarshal)
# yaml2json /tmp/run-instance.yml > /tmp/run-instance.json
# : create ec2 instance and security group with cloudformation
# aws cloudformation create-stack --stack-name $StackName --template-body file:///tmp/run-instance.json --parameters file:///tmp/parameters.json
# aws cloudformation wait stack-create-complete --stack-name $StackName
# : get output values like instance-id, and load into your shell
# aws cloudformation describe-stacks --stack-name $StackName | tee /tmp/output
# c=0; cat /tmp/output | jq -r '.Stacks[].Outputs[] | .OutputKey, .OutputValue' | while read -r x; do [[ $((c++ % 2)) -eq 0 ]] && echo -n $x || echo ="'"$x"'"; done | tee /tmp/vars
# . /tmp/vars
# ssh login to instance
# ssh -vi ~/.ssh/$KeyName ubuntu@$PublicDnsName
# clean up
# aws cloudformation delete-stack --stack-name $StackName && aws cloudformation wait stack-delete-complete --stack-name $StackName
Parameters:
KeyName:
Type: String
ImageId:
Type: String
InstanceType:
Type: String
Default: t2.micro
AllowSSHFromCidr:
Type: String
Default: 192.168.1.1/32
Outputs:
KeyName:
Value:
Ref: KeyName
InctanceId:
Value:
Ref: MyInstance
AvailabilityZone:
Value:
Fn::GetAtt:
- MyInstance
- AvailabilityZone
PublicDnsName:
Value:
Fn::GetAtt:
- MyInstance
- PublicDnsName
PrivateIp:
Value:
Fn::GetAtt:
- MyInstance
- PrivateIp
PublicIp:
Value:
Fn::GetAtt:
- MyInstance
- PublicIp
SecurityGroupId:
Value:
Fn::GetAtt:
- ServerSecurityGroup
- GroupId
Resources:
MyInstance:
Properties:
KeyName:
Ref: KeyName
ImageId:
Ref: ImageId
InstanceType:
Ref: InstanceType
SecurityGroups:
- Ref: ServerSecurityGroup
Type: AWS::EC2::Instance
ServerSecurityGroup:
Properties:
GroupDescription: allow connections from specified CIDR ranges
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: "80"
IpProtocol: tcp
ToPort: "80"
- CidrIp:
Ref: AllowSSHFromCidr
FromPort: "22"
IpProtocol: tcp
ToPort: "22"
Type: AWS::EC2::SecurityGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment