diff --git a/cloudformation/template.yml b/cloudformation/template.yml deleted file mode 100644 index d078483..0000000 --- a/cloudformation/template.yml +++ /dev/null @@ -1,140 +0,0 @@ ---- -AWSTemplateFormatVersion: '2010-09-09' -Description: 'Autoscaling Group with SNS/SQS Notifications' - -Parameters: - KeyName: - Description: SSH keypair used to access the instances - Type: AWS::EC2::KeyPair::KeyName - MinLength: 1 - - InstanceType: - Description: Instance type - Type: String - Default: t1.micro - MinLength: 1 - - SpotPrice: - Description: Spot bid price to use for the instances. 0 means normal (non-spot) instances - Type: String - Default: 0 - - Count: - Description: Instance Count - Type: Number - Default: 1 - - VpcId: - Type: String - Description: Id of an existing VPC to launch instances into. - - Subnets: - Type: CommaDelimitedList - Description: Comma separated list of two existing VPC subnet ids where EC2 instances will run. - -Conditions: - UseSpotInstances: - "Fn::Not": [ "Fn::Equals": [ { Ref: SpotPrice }, 0 ] ] - -Resources: - # Allow ec2 instances to assume a role and be granted the IAMPolicies - IAMInstanceProfile: - Type: AWS::IAM::InstanceProfile - Properties: - Path: / - Roles: [ !Ref IAMRole ] - - IAMRole: - Type: AWS::IAM::Role - Properties: - AssumeRolePolicyDocument: - Statement: - - Effect: Allow - Principal: - Service: [ ec2.amazonaws.com ] - Action: sts:AssumeRole - Path: / - - IAMPolicies: - Type: AWS::IAM::Policy - Properties: - PolicyName: InstancePolicy - PolicyDocument: - Statement: - - Effect: Allow - Action: - - autoscaling:DescribeAutoScalingInstances - - autoscaling:DescribeLifecycleHooks - - autoscaling:RecordLifecycleActionHeartbeat - - autoscaling:CompleteLifecycleAction - Resource: "*" - - Effect: Allow - Action: - - sqs:* - - sns:Unsubscribe - - sns:Subscribe - Resource: "*" - Roles: - - !Ref IAMRole - - LaunchConfiguration: - Type: AWS::AutoScaling::LaunchConfiguration - Properties: - AssociatePublicIpAddress: true - SecurityGroups: [ !Ref SecurityGroup ] - KeyName : !Ref KeyName - IamInstanceProfile: !Ref IAMInstanceProfile - InstanceType: !Ref InstanceType - ImageId: "ami-abc1ebbd" - SpotPrice: !If [ "UseSpotInstances", !Ref SpotPrice, !Ref 'AWS::NoValue' ] - - LifecycleTopic: - Type: AWS::SNS::Topic - - LifecycleHookRole: - Type: AWS::IAM::Role - Properties: - AssumeRolePolicyDocument: - Statement: - - Effect: Allow - Principal: - Service: [ autoscaling.amazonaws.com ] - Action: sts:AssumeRole - Policies: - - PolicyName: LifecyclePolicy - PolicyDocument: - Statement: - - Effect: Allow - Action: - - sns:Publish - Resource: !Ref LifecycleTopic - Path: / - - LifecycleHook: - Type: AWS::AutoScaling::LifecycleHook - Properties: - AutoScalingGroupName: !Ref AutoScaleGroup - LifecycleTransition: autoscaling:EC2_INSTANCE_TERMINATING - DefaultResult: CONTINUE - NotificationTargetARN: !Ref LifecycleTopic - RoleARN: !GetAtt LifecycleHookRole.Arn - - AutoScaleGroup: - Type: AWS::AutoScaling::AutoScalingGroup - Properties: - VPCZoneIdentifier: !Ref Subnets - LaunchConfigurationName: !Ref LaunchConfiguration - MinSize: 0 - DesiredCapacity: !Ref Count - MaxSize: 10 - - SecurityGroup: - Type: AWS::EC2::SecurityGroup - Properties: - GroupDescription: Enable access to SSH - VpcId: !Ref VpcId - SecurityGroupIngress: - - IpProtocol: tcp - FromPort: 22 - ToPort: 22 - CidrIp: 0.0.0.0/0 diff --git a/cloudformation/test.sh b/cloudformation/test.sh deleted file mode 100755 index b76d00b..0000000 --- a/cloudformation/test.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -set -euxo pipefail - -# configurable stuff -stack_name="lifecycled-test-$(date +%s)" -key_name="lox" -vpc_id="$1" -spot_price="${2:-0}" - -# lookup vpc config -subnets=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$vpc_id" --query "Subnets[*].[SubnetId,AvailabilityZone]" --output text) -subnet_ids=$(awk '{print $1}' <<< "$subnets" | tr ' ' ',' | tr '\n' ',' | sed 's/,$//') - -echo "Found vpc_id $vpc_id subnets $subnet_ids" - -echo "--- Creating stack ${stack_name}" -aws cloudformation create-stack \ - --output text \ - --stack-name "$stack_name" \ - --disable-rollback \ - --parameters \ - "ParameterKey=KeyName,ParameterValue=${key_name}" \ - "ParameterKey=VpcId,ParameterValue=${vpc_id}" \ - "ParameterKey=Subnets,ParameterValue=\"${subnet_ids}\"" \ - "ParameterKey=SpotPrice,ParameterValue=${spot_price}" \ - --template-body "file://${PWD}/cloudformation/template.yml" \ - --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM - -echo "--- Waiting for stack to complete" -aws cloudformation wait stack-create-complete --stack-name "${stack_name}"