Build a Complete Test Environment on AWS in Minutes with Cloud Formation

By  Maham Qasim

November 17, 2021 2:06 pm EST

Building a complete test environment on AWS in minutes sounds like an impossible task, right? Yet, thanks to Amazon Web Services (AWS) and Cloud Formation, it’s not only possible, but it’s easy!

What is AWS?

Amazon Web Services (AWS) is a suite of cloud computing tools that makes sharing information over the internet easy. 

With AWS, you can manage websites and other online services without worrying about setting up servers or maintaining backups; it offers 24/7 support and bills for usage only.

Why Choose AWS?

Running servers is time consuming and expensive– not to mention difficult. The traditional way of sharing information required many hours in setting up the machine(s) you want to run your service on and subsequently maintaining those machines, as new updates come out. 

Cloud computing in the form of AWS makes this a thing of the past– you can let them worry about patches and updates while you focus on developing your application.

What Can You Do With AWS?

At its core, AWS is a platform for sharing information over the internet easily and cost effectively. It offers you web services like Messaging (SNS), Hosting (S3), and Business Applications (DynamoDB). In turn, these services allow you to handle tasks that would normally require servers, like hosting a website or file storage.

How Does CloudFormation Work?

To get started with AWS, you need to create an account. Then, you can sign up for individual services or go straight to CloudFormation, the service that helps you create and manage entire stacks of AWS resources.

  1. First, sign into your AWS account.
  2. Next, you need to sign up for CloudFormation. Click “Create New Stack”. You can give your stack any name you want, but choose “Sample-S3SimpleSite” as your template because it’s simple enough for our purposes without being overwhelming.
  3. Download and install the AWS CLI with CloudFormation.
  4. Open the AWS CLI and run the following commands to get your credentials:

$ aws configure

  1. To create a stack, you need an S3 bucket that will act as the source for content that gets copied into the stack every time you create a stack. Run the following command:

$ aws s3api create-bucket –bucket yourBucketName

Note: Replace “yourBucketName”with your own unique name.

  1. Create a template. This is where CloudFormation actually builds the stack by running the following command:

$ aws cloudformation package –template-file teststack.yaml –s3-bucket yourBucketName

  1. The following command will guide you through creating your stack with the resources described in teststack.yaml:

$ aws cloudformation create-stack –template-body file://teststack.yaml –parameters ParameterKey=yourBucketName,ParameterValue=yourBucketName

  1. When the stack is created, AWS will give you a “StackId” which will look something like this:

arn:was:cloudformation:us-east-1::stack/YourStackName/f84d7a52-2c4b-4541-aaa8-be17e002fa28

Note: This StackId is unique to your AWS account, region, and the resources you are creating.

  1. Update the template file with the credentials and StackId for our newly created stack. Open teststack.yaml in a text editor and replace everything inside of ‘YourStackName’ with your own Stack ID:

$ cat teststack.yaml

  1.  Finally, create the stack again with the updated template file:

$ aws cloudformation create-stack –template-body file://teststack.yaml –parameters ParameterKey=yourBucketName,ParameterValue=yourBucketName

  1. Make sure everything worked by running the following command:

$ aws cloudformation describe-stacks –stack-name YourStackName

Outputs: 1 Output key=vpc_id Value=vpc-6e0c57e8

  1. You can now launch your EC2 instance. 
  2. Head back to the AWS Console and click on “EC2” under the “Compute” section of AWS. Select “Launch Instance” and find the “Classic Wizard” section, select “Quick Start”, and click on “Community AMIs.” 
  3. From here, search by operating system or choose one of the many pre-built AMIs available for Microsoft Windows Server 2008 R2/2012 R2 related to your application’s needs. Click on one that suits your application, specify your key pair if you have one, and click on “Review and Launch”.

Why Do You Need It?

Developing infrastructure as code enables you to create an environment for testing any application that mimics the live version, allowing for better test coverage and debugging before reaching customers.

You can also save time by reusing templates when developing new features. 

Conclusion

AWS CloudFormation can be used to create automated testing environments that are configured akin to production, in minutes, rather than hours or days. This type of fully automated workflow enables teams to manage their infrastructure as code, save money by reusing templates, and increase reliability by removing human error from the provisioning process altogether.


Maham Qasim

Maham is a copywriter and content creator who's always been drawn to the idea that there's more than one way of getting things done. Her writing career can be thought of as just another side hustle for her; when she isn't crafting content or reading Oscar Wilde, Maham often strategizes about how best to reach out with an engaging voice in this ever changing marketplace!

More like this