Skip to content

Commit

Permalink
Cloud9 Support for Layered Stacks (#224)
Browse files Browse the repository at this point in the history
* Cloud9 Support for Layered Stacks

* Cloud9 Support for Layered Stacks

* Typo Errors on Stack Names corrected

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Update content/intermediate/templates/layered-stacks/index.md

Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>

* Fix syntax

---------

Co-authored-by: Franco Rezabek <43790446+rezabekf@users.noreply.github.com>
Co-authored-by: Swopnil Dangol <swopnildangol@gmail.com>
  • Loading branch information
3 people committed May 16, 2023
1 parent 759534f commit 175bd64
Showing 1 changed file with 90 additions and 22 deletions.
112 changes: 90 additions & 22 deletions content/intermediate/templates/layered-stacks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -67,44 +67,79 @@ 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

##### 1. Prepare the IAM role template

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

Expand All @@ -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.'
Expand All @@ -133,7 +168,7 @@ Parameters:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Description: 'The ID of the AMI.'
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
```
:::

##### 3. Update WebServerInstance resource

Expand All @@ -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
{...}
Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 175bd64

Please sign in to comment.