1. Introduction

In this post, we will launch an EC2 instance by creating a Cloud Formation Stack. We will configure Cloud Formation Template to install java package. As an output, we will get a running EC2 instance with Java package installed.

2. Creating Cloud Formation Template

Let’s create a template that will launch an instance with java installation. You can use AWS CloudFormation Designer to create and validate templates.

First, we need to add a new resource to launch a new EC2 instance.

"EC2Instance": {
    "Type": "AWS::EC2::Instance",
    "Properties": {
        "ImageId": "ami-2581aa40",
        "InstanceType": "t2.micro",
        "KeyName": "Satish-Ohio"

Let’s change KeyName from your existing Public key under Key Pair section.

We selected Ubuntu AMI and used a Linux command line java installation. You are free to change ImageId and accordingly change Java installation command.

Next, we need to include UserData to run bash commands. In our case, we need to include Java installation command.

"UserData": {
    "Fn::Base64": {
        "Fn::Join": [
                "apt -y install openjdk-8-jre-headless\n"

You can download the complete template here: Launch Instance Template.

3. Creating Cloud Formation Stack

You can easily create a new stack from CloudFormation Console and select the template that we created in the last section. Follow AWS documentation for Creating a Stack from CloudFormation Console.

Wait for some minutes and AWS will launch a new instance with Java package installed. Check properties values under template in case your stack resulted in a rollback.

4. Assignment (Optional)

This assignment is about attaching an instance profile to EC2 instance. You must try to complete the assignment by yourself. In case you are facing issues, you can download the instance profile template.

First, you need to create an IAM role. Second, create a new instance profile. Finally, attach it to the EC instance.

All of these services should be configured under the Cloud Formation Template.

Now, create another stack with this new template.

In case you are facing issues, please download and match the solution template.

5. Conclusion

In this article, we’ve created a CloudFormation template to launch an EC2 instance with Java installation.

The full source can be found over on GitHub.

Launching Instance using Cloud Formation
Tagged on:     

Satish Pandey

I am an expert Java Spring Angular developer with 10+ years of rich and varied experience in developing end-to-end Web Applications. I maintain this blog and publish articles in my free time to help the community. Email: satish@cloudtechpro.com

Leave a Reply

Notify of