
Member-only story
How to Deploy EC2 Instances in AWS using Terraform
In this story, we will learn to deploy both Linux and Windows EC2 Instances in AWS using Terraform.
And also, we will learn how to deploy applications or configure settings at boot time (bootstrapping).
1. Requirements
To deploy a Virtual Machine in AWS, we will need:
- AWS Credentials
- Create an AWS Key Pair
- Define AWS and Terraform Providers
- Create a VPC, Subnet and other network components
- Create Operating System Versions Variables
- (Optional) Create a Bootstrapping script to install and/or configure applications
- Create a Security Group
- Create the EC2 Instance (Virtual Machine)
- (Optional) Request a Public IP and attach to the EC2 Instance
Note: For clarity, we prefer to define separate files, however, you can put together the code, using the traditional main.tf, variables.tf and output.tf layout.
2. AWS Credentials
Before creating our AWS EC2 Instance, we will need AWS Credentials to execute our Terraform code.
The AWS provider offers a few options of providing credentials for authentication:
- Static credentials
- Environment variables
- Shared credentials/configuration file
For this story, we will use static credentials. Please refer to the “How to create an IAM account and configure Terraform to use AWS static credentials?” story, if you need help to create the credentials.
Note: Using static credentials are great for learning and testing however hard-coded credentials are not recommended in production environments. Never push hard-coded credentials to code repositories.
3. AWS Key Pair
We will need an AWS Key Pair, consisting of a public key and a private key. The AWS Key Pair is a set of security credentials that we need to connect to an Amazon EC2 instance.