From 599ee8baf0181e99671ec36d64f28ee356cef9e0 Mon Sep 17 00:00:00 2001 From: David Chung Date: Sun, 7 Jan 2018 13:11:29 -0800 Subject: [PATCH] example aws playbook Signed-off-by: David Chung --- examples/playbooks/aws/README.md | 21 +++++++ examples/playbooks/aws/index.yml | 9 +++ examples/playbooks/aws/provision-instance.yml | 56 +++++++++++++++++++ .../playbooks/aws/provision-spot-instance.yml | 49 ++++++++++++++++ examples/playbooks/aws/start.sh | 26 +++++++++ 5 files changed, 161 insertions(+) create mode 100644 examples/playbooks/aws/README.md create mode 100644 examples/playbooks/aws/index.yml create mode 100644 examples/playbooks/aws/provision-instance.yml create mode 100644 examples/playbooks/aws/provision-spot-instance.yml create mode 100644 examples/playbooks/aws/start.sh diff --git a/examples/playbooks/aws/README.md b/examples/playbooks/aws/README.md new file mode 100644 index 000000000..bd658e94f --- /dev/null +++ b/examples/playbooks/aws/README.md @@ -0,0 +1,21 @@ +Example Playbook for AWS +======================== + +This playbook contains example of working with AWS. There are commands for +starting up infrakit (which assumes you have used AWS CLI on your local computer and +the `.aws/credentials` file exists) and commands for spinning up an on-demand or +spot instance configured with Docker, Git and Go compiler. + +## Adding this playbook + +Adding files locally: + +``` +infrakit playbook add aws file://$(pwd)/index.yml +``` + +Adding the playbook from Github: + +``` +infrakit playbook add aws https://raw.githubusercontent.com/docker/infrakit/master/examples/playbooks/aws/index.yml +``` diff --git a/examples/playbooks/aws/index.yml b/examples/playbooks/aws/index.yml new file mode 100644 index 000000000..55375b304 --- /dev/null +++ b/examples/playbooks/aws/index.yml @@ -0,0 +1,9 @@ + +# Start infrakit +start : start.sh + +# Create a single instance +ondemand : provision-instance.yml + +# Create a spot instance +spot : provision-spot-instance.yml \ No newline at end of file diff --git a/examples/playbooks/aws/provision-instance.yml b/examples/playbooks/aws/provision-instance.yml new file mode 100644 index 000000000..fda0b47e5 --- /dev/null +++ b/examples/playbooks/aws/provision-instance.yml @@ -0,0 +1,56 @@ +{{/* Input to create instance using the AWS instance plugin */}} +{{/* =% instanceProvision `aws/ec2-instance` %= */}} + +{{ $user := flag "user" "string" "username" | prompt "Please enter your user name:" "string" (env "USER")}} +{{ $project := flag "project" "string" "project" | prompt "Project?" "string" "myproject" }} +{{ $imageId := flag "image-id" "string" "Image ID" | prompt "AMI?" "string" "ami-df8406b0" }} +{{ $instanceType := flag "instance-type" "string" "instance type" | prompt "Instance type?" "string" "t2.micro" }} +{{ $privateIp := flag "private-ip" "string" "Private IP" | prompt "Private IP address (IPv4)?" "string" "172.31.20.100" }} + + +{{ $keyName := flag "key" "string" "ssh key name" | prompt "SSH key?" "string" "infrakit"}} +{{ $az := flag "az" "string" "availability zone" | prompt "Availability zone?" "string" "eu-central-1b"}} +{{ $subnetId := flag "subnet" "string" "subnet id" | prompt "Subnet ID?" "string" "subnet-11b8376a" }} +{{ $securityGroupId := flag "security-group-id" "string" "security group id" | prompt "Security group ID?" "string" "sg-975500fe" }} + +Tags: + infrakit.scope: {{ $project }} + infrakit.created: {{ now | htmlDate }} + infrakit.user: {{ $user }} + +Init: | + #!/bin/bash + sudo add-apt-repository ppa:gophers/archive + sudo apt-get update -y + sudo apt-get install -y wget curl git golang-1.9-go + wget -qO- https://get.docker.com | sh + ln -s /usr/lib/go-1.9/bin/go /usr/local/bin/go + +Properties: + RunInstancesInput: + BlockDeviceMappings: null + DisableApiTermination: null + EbsOptimized: null + IamInstanceProfile: null + ImageId: {{ $imageId }} + InstanceInitiatedShutdownBehavior: null + InstanceType: {{ $instanceType }} + KeyName: {{ $keyName }} + NetworkInterfaces: + - AssociatePublicIpAddress: true + PrivateIpAddress: {{ $privateIp }} + DeleteOnTermination: true + DeviceIndex: 0 + Groups: + - {{ $securityGroupId }} + NetworkInterfaceId: null + SubnetId: {{ $subnetId }} + Placement: + Affinity: null + AvailabilityZone: {{ $az }} + Tenancy: null + RamdiskId: null + SubnetId: null + UserData: null + Tags: + infrakit.user: {{ $user }} diff --git a/examples/playbooks/aws/provision-spot-instance.yml b/examples/playbooks/aws/provision-spot-instance.yml new file mode 100644 index 000000000..c910bb8fc --- /dev/null +++ b/examples/playbooks/aws/provision-spot-instance.yml @@ -0,0 +1,49 @@ +{{/* Input to create a spot instance using the AWS instance plugin */}} +{{/* =% instanceProvision `aws/ec2-spot-instance` %= */}} + +{{ $user := flag "user" "string" "username" | prompt "Please enter your user name:" "string" (env "USER")}} +{{ $project := flag "project" "string" "project" | prompt "Project?" "string" "myproject" }} +{{ $imageId := flag "image-id" "string" "Image ID" | prompt "AMI?" "string" "ami-df8406b0" }} +{{ $instanceType := flag "instance-type" "string" "instance type" | prompt "Instance type?" "string" "t2.micro" }} +{{ $privateIp := flag "private-ip" "string" "Private IP" | prompt "Private IP address (IPv4)?" "string" "172.31.20.100" }} +{{ $spotPrice := flag "spot-price" "string" "Spot price" | prompt "Spot price?" "string" "0.03" }} + +{{ $keyName := flag "key" "string" "ssh key name" | prompt "SSH key?" "string" "infrakit"}} +{{ $az := flag "az" "string" "availability zone" | prompt "Availability zone?" "string" "eu-central-1b"}} +{{ $subnetId := flag "subnet" "string" "subnet id" | prompt "Subnet ID?" "string" "subnet-11b8376a" }} +{{ $securityGroupId := flag "security-group-id" "string" "security group id" | prompt "Security group ID?" "string" "sg-975500fe" }} + +Tags: + infrakit.scope: {{ $project }} + infrakit.created: {{ now | htmlDate }} + infrakit.user: {{ $user }} + +Init: | + #!/bin/bash + sudo add-apt-repository ppa:gophers/archive + sudo apt-get update -y + sudo apt-get install -y wget curl git golang-1.9-go + wget -qO- https://get.docker.com | sh + ln -s /usr/lib/go-1.9/bin/go /usr/local/bin/go + +Properties: + RequestSpotInstancesInput: + LaunchSpecification: + ImageId: {{ $imageId }} + InstanceType: {{ $instanceType }} + KeyName: infrakit + NetworkInterfaces: + - AssociatePublicIpAddress: true + DeleteOnTermination: true + DeviceIndex: 0 + Groups: + - {{ $securityGroupId }} + NetworkInterfaceId: null + PrivateIpAddress: {{ $privateIp }} + PrivateIpAddresses: null + SecondaryPrivateIpAddressCount: null + SubnetId: {{ $subnetId }} + Placement: + AvailabilityZone: {{ $az }} + SpotPrice: "{{ $spotPrice }}" + Type: one-time diff --git a/examples/playbooks/aws/start.sh b/examples/playbooks/aws/start.sh new file mode 100644 index 000000000..81efc31a8 --- /dev/null +++ b/examples/playbooks/aws/start.sh @@ -0,0 +1,26 @@ +{{/* =% sh %= */}} + +{{ $profile := flag "aws-cred-profile" "string" "Profile name" | prompt "Profile for your .aws/credentials?" "string" "default" }} +{{ $region := flag "region" "string" "aws region" | prompt "Region?" "string" "eu-central-1"}} +{{ $project := flag "project" "string" "project name" | prompt "Project?" "string" "myproject" }} + +echo "Starting infrakit with aws plugin..." + +{{/* Pick a credential from the local user's ~/.aws folder. You should have this if you use awscli. */}} +{{ $creds := (source (cat "file://" (env "HOME") "/.aws/credentials" | nospace) | iniDecode | k $profile ) }} +FOUND="{{ not (empty $creds) }}" + +if [ $FOUND = "false" ]; then + echo "no credentials found. bye" + exit 1 +fi + +{{ echo "Found your credential for profile" $profile }} + +AWS_ACCESS_KEY_ID={{ $creds.aws_access_key_id }} \ +AWS_SECRET_ACCESS_KEY={{ $creds.aws_secret_access_key }} \ +INFRAKIT_AWS_REGION={{ $region }} \ +INFRAKIT_AWS_STACK_NAME={{ $project }} \ +INFRAKIT_AWS_NAMESPACE_TAGS="infrakit.scope={{ $project }}" \ +INFRAKIT_AWS_MONITOR_POLL_INTERVAL=5s \ +infrakit plugin start aws