From 7e07c354744c8467890f74d1a63efed259585ed5 Mon Sep 17 00:00:00 2001 From: Jack Ivanov <17044561+jackivanov@users.noreply.github.com> Date: Sat, 3 Mar 2018 00:13:49 +0300 Subject: [PATCH] proper cloudformation template (#815) --- .../stack.yml.j2 => files/stack.yml} | 27 ++++++++++++------- roles/cloud-ec2/tasks/cloudformation.yml | 14 +++++----- 2 files changed, 24 insertions(+), 17 deletions(-) rename roles/cloud-ec2/{templates/stack.yml.j2 => files/stack.yml} (88%) diff --git a/roles/cloud-ec2/templates/stack.yml.j2 b/roles/cloud-ec2/files/stack.yml similarity index 88% rename from roles/cloud-ec2/templates/stack.yml.j2 rename to roles/cloud-ec2/files/stack.yml index 694386f..7f814e3 100644 --- a/roles/cloud-ec2/templates/stack.yml.j2 +++ b/roles/cloud-ec2/files/stack.yml @@ -1,13 +1,19 @@ --- - AWSTemplateFormatVersion: '2010-09-09' Description: 'Algo VPN stack' +Parameters: + InstanceTypeParameter: + Type: String + Default: t2.micro + PublicSSHKeyParameter: + Type: String + ImageIdParameter: + Type: String Resources: - VPC: Type: AWS::EC2::VPC Properties: - CidrBlock: {{ ec2_vpc_nets.cidr_block }} + CidrBlock: 172.16.0.0/16 EnableDnsSupport: true EnableDnsHostnames: true InstanceTenancy: default @@ -35,7 +41,7 @@ Resources: Subnet: Type: AWS::EC2::Subnet Properties: - CidrBlock: {{ ec2_vpc_nets.subnet_cidr }} + CidrBlock: 172.16.254.0/23 MapPublicIpOnLaunch: false Tags: - Key: Environment @@ -148,16 +154,19 @@ Resources: homeDir: "/home/ubuntu/" files: /home/ubuntu/.ssh/authorized_keys: - content: {{ lookup('file', SSH_keys.public) }} + content: + Ref: PublicSSHKeyParameter mode: "000644" owner: "ubuntu" group: "ubuntu" Properties: - InstanceType: {{ cloud_providers.ec2.size }} + InstanceType: + Ref: InstanceTypeParameter InstanceInitiatedShutdownBehavior: terminate SecurityGroupIds: - Ref: InstanceSecurityGroup - ImageId: {{ ami_image }} + ImageId: + Ref: ImageIdParameter SubnetId: !Ref Subnet Ipv6AddressCount: 1 UserData: @@ -176,8 +185,8 @@ Resources: apt-get update apt-get -y install python-setuptools easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz - cfn-init -v --stack {{ stack_name }} --resource EC2Instance --region {{ region }} - cfn-signal -e $? --stack {{ stack_name }} --resource EC2Instance --region {{ region }} + cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region} + cfn-signal -e $? --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region} Tags: - Key: Name Value: Algo diff --git a/roles/cloud-ec2/tasks/cloudformation.yml b/roles/cloud-ec2/tasks/cloudformation.yml index 1f24b00..032a59b 100644 --- a/roles/cloud-ec2/tasks/cloudformation.yml +++ b/roles/cloud-ec2/tasks/cloudformation.yml @@ -1,10 +1,4 @@ --- - -- name: Make a cloudformation template - template: - src: stack.yml.j2 - dest: "configs/{{ aws_server_name }}.yml" - - name: Deploy the template cloudformation: aws_access_key: "{{ aws_access_key | default(lookup('env','AWS_ACCESS_KEY_ID'), true)}}" @@ -12,7 +6,11 @@ stack_name: "{{ stack_name }}" state: "present" region: "{{ region }}" - template: "configs/{{ aws_server_name }}.yml" + template: roles/cloud-ec2/files/stack.yml + template_parameters: + InstanceTypeParameter: "{{ cloud_providers.ec2.size }}" + PublicSSHKeyParameter: "{{ lookup('file', SSH_keys.public) }}" + ImageIdParameter: "{{ ami_image }}" tags: Environment: Algo - register: stack \ No newline at end of file + register: stack