diff --git a/content/intermediate/templates/layered-stacks/index.md b/content/intermediate/templates/layered-stacks/index.md index 567f0217e..97f82916a 100644 --- a/content/intermediate/templates/layered-stacks/index.md +++ b/content/intermediate/templates/layered-stacks/index.md @@ -51,7 +51,7 @@ If you look in the file `vpc.yaml` file, you will notice that there are some out Add the lines [4-5, 9-10 and 14-15] to your template file: -```yaml {hl_lines=[4,5,9,10,14,15]} +:::code{language=yaml showLineNumbers=true showCopyAction=true highlightLines=4-5,9-10,14-15} Outputs: VpcId: Value: !Ref VPC @@ -67,19 +67,37 @@ Outputs: Value: !Ref VPCPublicSubnet2 Export: Name: cfn-workshop-PublicSubnet2 -``` +::: ##### 2. Deploy the VPC Stack - +:::::tabs{variant="container"} +::::tab{id="cloud9" label="Cloud9"} +1. In the **Cloud9 terminal** navigate to `cfn101-workshop/code/workspace/layered-stacks`. +1. **Create Stack** by using the following AWS CLI command. The template requires you provide the values for `AvailabilityZones` parameter, For example `us-east-1a` and `us-east-1b` are used below. Please select 2 Availability Zone based on your region. +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation create-stack \ +--stack-name cfn-workshop-layered-stacks-vpc \ +--template-body file://vpc.yaml \ +--parameters ParameterKey=AvailabilityZones,ParameterValue=us-east-1a\\,us-east-1b +::: +1. Wait until the stack creation is completed by running the following AWS CLI command +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation wait stack-create-complete \ +--stack-name cfn-workshop-layered-stacks-vpc +::: +:::: +::::tab{id="local" label="Local development"} 1. Navigate to CloudFormation in the console and click **Create stack With new resources (standard)**. 1. In **Prepare template** select **Template is ready**. 1. In **Template source** select **Upload a template file**. 1. Choose a file `vpc.yaml`. -1. Enter a **stack name**. For example, `cfn-workshop-vpc`. +1. Enter a **stack name**. For example, `cfn-workshop-layered-stacks-vpc`. 1. For the **AvailabilityZones** parameter, select **2 AZs**. 1. You can leave the rest of the parameters **default**. 1. Navigate through the wizard leaving everything default. -1. On the Review page, scroll down to the bottom and click on **Create stack**. +1. On the Review page, scroll down to the bottom and click on **Submit**. +:::: +::::: #### Create IAM Stack @@ -87,24 +105,41 @@ Outputs: 1. Open `iam.yaml` file. 1. Copy the lines [4-5] to the **Outputs** section of the template: - ```yaml {hl_lines=[4,5]} +:::code{language=yaml showLineNumbers=true showCopyAction=true highlightLines=4-5} Outputs: WebServerInstanceProfile: Value: !Ref WebServerInstanceProfile Export: Name: cfn-workshop-WebServerInstanceProfile - ``` +::: ##### 2. Deploy the IAM Stack - +:::::tabs{variant="container"} +::::tab{id="cloud9" label="Cloud9"} +1. Let's **Create Stack** by using the following AWS CLI command. The template requires you to specify `CAPABILITY_IAM` capability for creating IAM resources. +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation create-stack \ +--stack-name cfn-workshop-layered-stacks-iam \ +--template-body file://iam.yaml \ +--capabilities CAPABILITY_IAM +::: +1. Wait until the stack creation is completed by running the following AWS CLI command +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation wait stack-create-complete \ +--stack-name cfn-workshop-layered-stacks-iam +::: +:::: +::::tab{id="local" label="Local development"} 1. Navigate to CloudFormation in the console and click **Create stack With new resources (standard)**. 1. In **Prepare template** select **Template is ready**. 1. In **Template source** select **Upload a template file**. 1. Choose a file `iam.yaml`. -1. Enter a **stack name**. For example, `cfn-workshop-iam`. +1. Enter a **stack name**. For example, `cfn-workshop-layered-stacks-iam`. 1. Click **Next**. 1. Navigate through the wizard leaving everything default. -1. **Acknowledge IAM capabilities** and click on **Create stack**. +1. **Acknowledge IAM capabilities** and click on **Submit**. +:::: +::::: #### Create EC2 Layered Stack @@ -117,7 +152,7 @@ The concept of the **Layered Stack** is to use intrinsic functions to import pre Update the **Parameters** section to look as follows: -```yaml +:::code{language=yaml showLineNumbers=true showCopyAction=true} Parameters: EnvironmentType: Description: 'Specify the Environment type of the stack.' @@ -133,7 +168,7 @@ Parameters: Type: AWS::SSM::Parameter::Value Description: 'The ID of the AMI.' Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 -``` +::: ##### 3. Update WebServerInstance resource @@ -142,7 +177,7 @@ We perform this import by using the [Fn::ImportValue](https://docs.aws.amazon.co Update WebServerInstance resource in the Resources section of the `ec2.yaml` template: -```yaml +:::code{language=yaml showLineNumbers=true showCopyAction=true highlightLines=5-8} WebServerInstance: Type: AWS::EC2::Instance {...} @@ -152,12 +187,12 @@ WebServerInstance: ImageId: !Ref AmiID InstanceType: !FindInMap [EnvironmentToInstanceType, !Ref EnvironmentType, InstanceType] {...} -``` +::: ##### 4. Update the security group Finally, update the security group resource similarly. Update `WebServerSecurityGroup` resource in the **Resources** section of the `ec2.yaml` template, line [19]. -```yaml {hl_lines=[19]} +:::code{language=yaml showLineNumbers=true showCopyAction=true highlightLines=19} WebServerSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: @@ -177,18 +212,34 @@ WebServerSecurityGroup: ToPort: 443 CidrIp: 0.0.0.0/0 VpcId: !ImportValue cfn-workshop-VpcId -``` +::: ##### 5. Deploy the EC2 Stack - +:::::tabs{variant="container"} +::::tab{id="cloud9" label="Cloud9"} +1. Let's **Create Stack** by using the following AWS CLI command. The template requires you to specify `CAPABILITY_IAM` capability for creating IAM resources. +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation create-stack \ +--stack-name cfn-workshop-layered-stacks-ec2 \ +--template-body file://ec2.yaml +::: +1. Wait until the stack creation is completed by running the following AWS CLI command +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation wait stack-create-complete \ +--stack-name cfn-workshop-layered-stacks-ec2 +::: +:::: +::::tab{id="local" label="Local development"} 1. Navigate to CloudFormation in the console and click **Create stack With new resources (standard)**. 1. In **Prepare template** select **Template is ready**. 1. In **Template source** select **Upload a template file**. 1. Choose a file `ec2.yaml`. -1. Enter a **stack name**. For example, `cfn-workshop-ec2`. +1. Enter a **stack name**. For example, `cfn-workshop-layered-stacks-ec2`. 1. You can leave the rest of the parameters **default**. 1. Navigate through the wizard leaving everything default. -1. On the **Review page**, scroll down to the bottom and click on **Create stack**. +1. On the **Review page**, scroll down to the bottom and click on **Submit**. +:::: +::::: #### 7. Test the deployment @@ -212,14 +263,31 @@ If you not sure how to do that, follow the instructions from the [Session Manage For example, you can not delete the **VPC stack** before you delete **EC2 stack**. You get following error message: ![delete-export-before-import.png](/static/intermediate/templates/layered-stacks/delete-export-before-import.png) - -1. In the **[CloudFormation console](https://console.aws.amazon.com/cloudformation)**, select the **EC2 stack**, for example `cfn-workshop-ec2`. +:::::tabs{variant="container"} +::::tab{id="cloud9" label="Cloud9"} +1. **Delete Stack** by running the following AWS CLI command +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation delete-stack \ +--stack-name cfn-workshop-layered-stacks-ec2 +::: +1. Wait until the stack is deleted by using the following AWS CLI command. +:::code{language=shell showLineNumbers=false showCopyAction=true} +aws cloudformation wait stack-delete-complete \ +--stack-name cfn-workshop-layered-stacks-ec2 +::: +1. Repeat steps (1-2) above for stacks: `cfn-workshop-layered-stacks-iam` and `cfn-workshop-layered-stacks-vpc`. +:::: +::::tab{id="local" label="Local development"} +1. In the **[CloudFormation console](https://console.aws.amazon.com/cloudformation)**, select the **EC2 stack**, for example `cfn-workshop-layered-stacks-ec2`. 1. In the top right corner, click on **Delete**. -1. In the pop-up window click on **Delete stack**. +1. In the pop-up window click on **Delete**. 1. Hit the **refresh** button a few times until you see in the status **DELETE_COMPLETE**. 1. Now you can delete **IAM** and **VPC** stack in any order as there are no more dependencies. +:::: +::::: --- + ### Conclusion **Layered stacks** allow you to create resources that can be used again and again in multiple stacks. All the stack needs to know is the **Export** name used. They allow the separation of roles and responsibilities. For example, a network team