Skip to content
Binbash

Terraform Module: AWS EC2 Basic Layout

Overview

This module could be useful if you find yourself creating a layout that includes the following resources:

  • EC2 Instance w/ configurable AMI, family type, key pair, networking, userdata, among others.
  • EC2 Profile with a customizable IAM Role supporting both AWS and customer managed policies.
  • Multiple EBS dynamic blocks (root_block_device, ebs_block_device and ephemeral_block_device) w/ configurable type, size, device name and encryption configs among others.
  • Security group for the instance above.
  • Optionally associate a public IP address with the instance.
  • DNS record with a record that points to the instance private IP / public IP.
  • Tags: both EC2 and EBS.
  • SSM support for interactive shell access via browser or AWS CLI

Personally we have seen the need of creating a similar set of such resources for an OpenVPN instance, for Jenkins, Spinnaker, DroneCI, Prometheus, Grafana, Hashicorp Vault, ElasticSearch, Kibana and so forth.

Requirements

NameVersion
terraform>= 0.13.2
aws~> 4.0

Providers

NameVersion
aws~> 4.0

Modules

No modules.

Resources

NameType
aws_eip.thisresource
aws_iam_instance_profile.basic_instanceresource
aws_iam_policy.cross_org_instance_accessresource
aws_iam_role.basic_instance_assume_roleresource
aws_iam_role_policy_attachment.basic_instance_aws_rolesresource
aws_iam_role_policy_attachment.ec2_ssm_accessresource
aws_iam_role_policy_attachment.thisresource
aws_instance.mainresource
aws_route53_record.main_privateresource
aws_route53_record.main_publicresource
aws_security_group.mainresource
aws_security_group_rule.egress_allow_allresource
aws_security_group_rule.ingress_rulesresource
aws_ami.ubuntu_linuxdata source
aws_iam_policy_document.cross_org_instance_accessdata source
aws_iam_policy_document.thisdata source

Inputs

NameDescriptionTypeDefaultRequired
ami_idAMI Identifierstring""no
associate_public_ip_addressAssociate a public IP address with the instanceboolfalseno
aws_ami_os_idAWS AMI Operating System Identificatorstring"ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"no
aws_ami_os_ownerAWS AMI Operating System Owner, eg: 099720109477 for Canonicalstring"099720109477"no
credit_specification_cpuCan be applied/modified to the EC2 at any time. The credit option for CPU usage. Can be 'standard' or 'unlimited'. By default T3 = unlimited & T2 'standard'.string"unlimited"no
cross_account_roles_resource_arn_listResources arn list for cross org roles for EC2 profile IAM Role policy.list(string)[]no
disable_api_terminationIf true, enables EC2 Instance Termination Protectionstring"false"no
dns_records_internal_hosted_zoneA list of DNS private (internal hosted zone) records to create with the instance's IPlist(any)[]no
dns_records_public_hosted_zoneA list of DNS public (public hosted zone) records to create with the instance's IPlist(any)[]no
ebs_block_deviceAdditional EBS block devices to attach to the instancelist(map(string))[]no
ebs_optimizedEnable EBS Optimizedstring"false"no
enable_ssm_accessIf true, attaches SSM policy to instance roleboolfalseno
ephemeral_block_deviceCustomize Ephemeral (also known as Instance Store) volumes on the instancelist(map(string))[]no
instance_profileThe IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile.string""no
instance_typeEC2 Instance Typestring"t3.micro"no
key_pair_nameKey Pair Namestringn/ayes
monitoringIf true, the launched EC2 instance will have detailed monitoring enabledboolfalseno
nameNamestringn/ayes
policy_arnAttach AWS IAM managed policies to the IAM Role.list(string)[]no
prefixPrefixstring"default"no
root_block_deviceCustomize details about the root block device of the instance. See Block Devices below for detailslist(map(string))[]no
root_device_backup_tagEC2 Root Block Device backup tagstring"True"no
security_group_idsA list of security group idslist(string)[]no
security_group_rulesA list of security group ruleslist(any)[]no
subnet_idSubnet IDstringn/ayes
tag_approved_ami_valueSet the specific tag ApprovedAMI ('true' | 'false') that identifies aws-config compliant AMIsstring"false"no
tagsTagsmap(string){}no
user_dataThe user data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user_data_base64 instead.stringnullno
user_data_base64Can be used instead of user_data to pass base64-encoded binary data directly. Use this instead of user_data whenever the value is not a valid UTF-8 string. For example, gzip-encoded user data must be base64-encoded and passed via this argument to avoid corruption.stringnullno
vpc_idVPC IDstringn/ayes

Outputs

NameDescription
aws_instance_amiThe AMI of the Instance.
aws_instance_assume_role_nameThe IAM instance profile of the EC2.
aws_instance_iam_profileThe IAM instance profile of the EC2.
aws_instance_key_nameThe ssh key pair name of the Instance.
aws_instance_private_ipContains the instance private IP address.
aws_instance_public_ipContains the instance public IP address.
aws_instance_typeThe type of the Instance.
aws_instance_volume_tagsThe root EBS volume tags of the instace.
dns_record_privateDNS
dns_record_publicn/a
instanceCompute
security_groupn/a

Examples

Please refer to the module Examples.

Binbash Leverage | DevOps Automation Code Library Integration

In order to get the full automated potential of the Binbash Leverage DevOps Automation Code Library
you should initialize all the necessary helper Makefiles.

How?

You must execute the make init-makefiles command at the root context:

╭─delivery at delivery-I7567 in~/terraform/terraform-aws-backup-by-tags on master✔ 20-09-17
╰─⠠⠵ make
Available Commands:
- init-makefiles initialize makefiles

Why?

You'll get all the necessary commands to automatically operate this module via a dockerized approach, example shown below

╭─delivery at delivery-I7567 in~/terraform/terraform-aws-backup-by-tags on master✔ 20-09-17
╰─⠠⠵ make
Available Commands:
- circleci-validate-config ## Validate A CircleCI Config (https
- format-check ## The terraform fmt is used to rewrite tf conf files to a canonical format and style.
- format ## The terraform fmt is used to rewrite tf conf files to a canonical format and style.
- tf-dir-chmod ## run chown in ./.terraform to gran that the docker mounted dir has the right permissions
- version ## Show terraform version
- init-makefiles ## initialize makefiles
╭─delivery at delivery-I7567 in~/terraform/terraform-aws-backup-by-tags on master✔ 20-09-17
╰─⠠⠵ make format-check
docker run --rm -v /home/delivery/Binbash/repos/Leverage/terraform/terraform-aws-backup-by-tags:"/go/src/project/":rw -v :/config -v /common.config:/common-config/common.config -v ~/.ssh:/root/.ssh -v ~/.gitconfig:/etc/gitconfig -v ~/.aws/bb:/root/.aws/bb -e AWS_SHARED_CREDENTIALS_FILE=/root/.aws/bb/credentials -e AWS_CONFIG_FILE=/root/.aws/bb/config --entrypoint=/bin/terraform -w "/go/src/project/" -it binbash/terraform-awscli-slim:0.12.28 fmt -check

Release Management

CircleCi PR auto-release job

circleci

About

Terraform module to deploy a typical EC2 layout that includes an instance with a security group and a dns record.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

5 stars

Watchers

10 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages