AWS Cloud Practitioner CLF-C02
Technology Part Two
AWS Management Demo
Hi, welcome back Cloud Practitioners!
In this lesson, we will walk through a short demo of AWS CloudFormation—a service that accelerates cloud provisioning by defining your infrastructure as code. Essentially, you create a YAML or JSON file that describes the desired AWS resources, and CloudFormation uses that file to automatically provision and configure them.
Let's dive into a simple CloudFormation example.
A Simple CloudFormation Template
Consider the following basic CloudFormation template. This file consists solely of a Resources section that defines an S3 bucket by specifying its type and a single property: its bucket name.
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: "mydembucket10230661977"
The bucket name is arbitrary—you can name it "bucket99" or any valid name. When you run this template in the AWS Management Console, CloudFormation leverages the region defined in your console session to create the bucket.
For example, launching the stack might use a YAML snippet like:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: "mydembucket120236191977"
This concise definition, just a few lines of YAML, shows how simple it is to define and provision a resource.
Creating the Stack in the Console
Switch to the AWS Management Console and navigate to the CloudFormation service. CloudFormation is AWS’s native management tool for infrastructure as code. To create your new stack:
- Choose "Create Stack" with new resources.
- Select "Upload a template file" and click "Choose file" to locate your simple S3 bucket template.
- After uploading, click "Next."
Assign a stack name, such as "mys3demostack." Since the template contains no parameters, simply click "Next."
Scroll down, click "Next" again, then confirm your settings and submit your stack creation request. The simple five-line template starts provisioning your defined S3 bucket.
You can monitor the progress in the CloudFormation stack’s Events tab, which logs each action during the creation process. In a separate tab, navigate to the Amazon S3 dashboard to verify the presence of your new bucket. Even if there was a previously created demo bucket, the new bucket—named as per your file—should now appear.
After refreshing your CloudFormation stack view, you should see that the bucket creation is complete. To confirm, switch back to the S3 console and refresh the page—your bucket (for example, "2023-06-19-777") should be visible.
Since no objects have been uploaded to the bucket, its contents appear empty. More advanced CloudFormation templates can include Outputs or even automatically create and manage objects within the bucket, but this simple example clearly illustrates the foundational process.
Key Takeaway
With just a few lines of YAML, CloudFormation allows you to deploy and manage AWS resources effortlessly.
An Example of a More Complex Template
CloudFormation is capable of much more than simple resource creation. Here’s an example of a more complex template that demonstrates the use of conditions for access control, encryption, custom KMS keys, and outputs for the bucket's URL and ARN:
Conditions:
hasACL:
!Not
- !Ref ACL
- "None"
isEncrypted:
!Equals
- !Ref Encryption
- "true"
hasKmsKey:
!Not
- !Equals
- !Ref KmsKeyId
- "12345678-aaaa-bbbb-cccc-123456789abc"
useCustomKey:
!And
- Condition: isEncrypted
- Condition: hasKmsKey
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: !If
- hasACL
- !Ref ACL
- !Ref "AWS::NoValue"
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: !If
- useCustomKey
- !Ref kmsKeyId
- "aws:kms"
- AES256
BucketName: !Ref BucketName
VersioningConfiguration:
Status: !Ref Versioning
Outputs:
BucketURL:
Value: !Join
- ""
-
- "https://"
- !GetAtt Bucket.DomainName
Export:
Name: !Join
- ""
-
- !Ref AWS::StackName
- "BucketURL"
BucketARN:
Value: !GetAtt Bucket.Arn
Export:
Name: !Join
- ""
-
- !Ref AWS::StackName
- "BucketARN"
This example illustrates how CloudFormation can manage configurations for a variety of AWS services beyond S3, including EC2 and VPCs, by starting with simple constructs and progressively adding complexity.
Deleting the Stack
To remove the resources created by your CloudFormation stack, return to the CloudFormation console, select your stack, and click "Delete." Confirm the deletion process, and the removal will commence immediately.
Important
If the S3 bucket contains any objects, CloudFormation will prompt you to delete them before the stack can be deleted.
Conclusion
This demo shows that even a straightforward CloudFormation template—merely a few lines of YAML—can effectively manage AWS resources. CloudFormation is a powerful tool for infrastructure as code that scales from simple deployments to complex, multi-resource architectures.
Thank you for following along in this lesson. Michael Forrester, I look forward to seeing you in the next article.
Watch Video
Watch video content