AWS EC2 Instance Terraform module
Terraform module which creates an EC2 instance on AWS.
Usage
Single EC2 Instance
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
name = "single-instance"
instance_type = "t3.micro"
key_name = "user1"
monitoring = true
subnet_id = "subnet-eddcdzz4"
tags = {
Terraform = "true"
Environment = "dev"
}
}
Multiple EC2 Instance
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
for_each = toset(["one", "two", "three"])
name = "instance-${each.key}"
instance_type = "t3.micro"
key_name = "user1"
monitoring = true
subnet_id = "subnet-eddcdzz4"
tags = {
Terraform = "true"
Environment = "dev"
}
}
Spot EC2 Instance
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
name = "spot-instance"
create_spot_instance = true
spot_price = "0.60"
spot_type = "persistent"
instance_type = "t3.micro"
key_name = "user1"
monitoring = true
subnet_id = "subnet-eddcdzz4"
tags = {
Terraform = "true"
Environment = "dev"
}
}
Examples
Make an encrypted AMI for use
This module does not support encrypted AMI's out of the box however it is easy enough for you to generate one for use
This example creates an encrypted image from the latest ubuntu 20.04 base image.
provider "aws" {
region = "us-west-2"
}
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["679593333241"]
filter {
name = "name"
values = ["ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
resource "aws_ami_copy" "ubuntu_encrypted_ami" {
name = "ubuntu-encrypted-ami"
description = "An encrypted root ami based off ${data.aws_ami.ubuntu.id}"
source_ami_id = data.aws_ami.ubuntu.id
source_ami_region = "eu-west-2"
encrypted = true
tags = { Name = "ubuntu-encrypted-ami" }
}
data "aws_ami" "encrypted-ami" {
most_recent = true
filter {
name = "name"
values = [aws_ami_copy.ubuntu_encrypted_ami.id]
}
owners = ["self"]
}
Conditional creation
The following combinations are supported to conditionally create resources:
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
# Disable creation of EC2 and all resources
create = false
# Enable creation of spot instance
create_spot_instance = true
# Enable creation of EC2 IAM instance profile
create_iam_instance_profile = true
# Disable creation of security group
create_security_group = false
# Enable creation of elastic IP
create_eip = true
# ... omitted
}
Notes
network_interfacecan't be specified together withvpc_security_group_ids,associate_public_ip_address,subnet_id. See complete example for details.- In regards to spot instances, you must grant the
AWSServiceRoleForEC2Spotservice-linked role access to any custom KMS keys, otherwise your spot request and instances will fail withbad parameters. You can see more details about why the request failed by using the awscli andaws ec2 describe-spot-instance-requests
Authors
Module is maintained by Anton Babenko with help from these awesome contributors.
License
Apache 2 Licensed. See LICENSE for full details.
Additional information for users from Russia and Belarus
- Russia has illegally annexed Crimea in 2014 and brought the war in Donbas followed by full-scale invasion of Ukraine in 2022.
- Russia has brought sorrow and devastations to millions of Ukrainians, killed hundreds of innocent people, damaged thousands of buildings, and forced several million people to flee.
- Putin khuylo!