terraform-aws-arc-ec2¶
Overview¶
For more information about this repository and its usage, please see Terraform AWS ARC GitHub EC2 Module Usage Guide.
Introduction¶
SourceFuse's AWS Reference Architecture (ARC) Terraform module simplifies the creation and management of essential AWS infrastructure components. It is designed to provision and configure the following resources:
- EC2 Instances: Deploys and manages one or more EC2 instances, with customizable instance types, AMIs, key pairs, and network configurations.
- Volumes: Attaches EBS volumes to the EC2 instances for persistent storage, with options to specify volume size, type, and IOPS.
- Load Balancer: Creates an Elastic Load Balancer (ELB) to distribute incoming traffic across multiple EC2 instances, ensuring high availability and reliability. It supports both application (ALB) and network load balancers (NLB).
- Security Groups: Defines and manages security groups to control inbound and outbound traffic to the EC2 instances and load balancers, enhancing network security.
- EC2 Instance Profile: Creates and attaches an instance profile with IAM roles and policies to the EC2 instances, allowing them to interact with other AWS services securely.
Prerequisites¶
Before using this module, ensure you have the following:
- AWS credentials configured.
- Terraform installed.
- A working knowledge of Terraform.
Getting Started¶
- Define the Module
Initially, it's essential to define a Terraform module, which is organized as a distinct directory encompassing Terraform configuration files. Within this module directory, input variables and output values must be defined in the variables.tf and outputs.tf files, respectively. The following illustrates an example directory structure:
- Define Input Variables
Inside the variables.tf
or in *.tfvars
file, you should define values for the variables that the module requires.
- Use the Module in Your Main Configuration In your main Terraform configuration file (e.g., main.tf), you can use the module. Specify the source of the module, and version, For Example
- Output Values
Inside the outputs.tf
file of the module, you can define output values that can be referenced in the main configuration. For example:
- .tfvars
Inside the .tfvars
file of the module, you can provide desired values that can be referenced in the main configuration. For example:
Edit the locals.tf file and provide desired values.
security_group_data
- Security Group details for EC2 instance
instance_profile_data
- EC2 instance profile
additional_ebs_volumes
- Additional EBS volume details
target_groups
- Target Group details for Load balancer
load_balancer_data
- Details to create Load balancer
First Time Usage¶
uncomment the backend block in main.tf
terraform init
should be fine
Create a dev
workspace
Plan Terraform
Apply Terraform
Production Setup¶
Create a prod
workspace
Plan Terraform
Apply Terraform
Cleanup¶
Destroy Terraform
Requirements¶
Name | Version |
---|---|
terraform | >= 1.5.0 |
aws | ~> 5.0 |
Providers¶
Name | Version |
---|---|
aws | 5.57.0 |
Modules¶
Name | Source | Version |
---|---|---|
load_balancer | ./modules/alb | n/a |
Resources¶
Name | Type |
---|---|
aws_ebs_volume.this | resource |
aws_eip.this | resource |
aws_iam_instance_profile.this | resource |
aws_iam_role.this | resource |
aws_instance.this | resource |
aws_security_group.this | resource |
aws_volume_attachment.this | resource |
Inputs¶
Name | Description | Type | Default | Required |
---|---|---|---|---|
additional_ebs_volumes | (optional) ebs_block_device block supports the following:name - (Optional) Name of the volume delete_on_termination - (Optional) Whether the volume should be destroyed on instance termination. Defaults to true. device_name - (Required) Name of the device to mount. encrypted - (Optional) Enables EBS encryption on the volume. Defaults to false. Cannot be used with snapshot_id. Must be configured to perform drift detection. iops - (Optional) Amount of provisioned IOPS. Only valid for volume_type of io1, io2 or gp3. kms_key_id - (Optional) Amazon Resource Name (ARN) of the KMS Key to use when encrypting the volume. Must be configured to perform drift detection. snapshot_id - (Optional) Snapshot ID to mount. tags - (Optional) Map of tags to assign to the device. throughput - (Optional) Throughput to provision for a volume in mebibytes per second (MiB/s). This is only valid for volume_type of gp3. volume_size - (Optional) Size of the volume in gibibytes (GiB). volume_type - (Optional) Type of volume. Valid values include standard, gp2, gp3, io1, io2, sc1, or st1. Defaults to gp2. Device name : https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html |
map(object({ |
{} |
no |
ami_id | The AMI ID for the EC2 instance. | string |
n/a | yes |
assign_eip | (optional) Whether to assign Elastic IP address, note associate_public_ip_address has to be enabled |
bool |
false |
no |
associate_public_ip_address | Associate a public IP address with the instance. | bool |
false |
no |
ebs_optimized | (optional) If true, the launched EC2 instance will be EBS-optimized. Note that if this is not set on an instance type that is optimized by default then this will show as disabled but if the instance type is optimized by default then there is no need to set this and there is no effect to disabling it. | bool |
false |
no |
enable_detailed_monitoring | (optional) Whether to enable detailed monitoring | bool |
false |
no |
enable_stop_protection | (optional) If true, enables EC2 Instance Stop Protection. | bool |
false |
no |
enable_termination_protection | (optional) If true, enables EC2 Instance Termination Protection. | bool |
false |
no |
instance_metadata_options | The metadata_options block supports the following: http_endpoint - (Optional) Whether the metadata service is available. Valid values include enabled or disabled. Defaults to enabled. http_protocol_ipv6 - (Optional) Whether the IPv6 endpoint for the instance metadata service is enabled. Defaults to disabled. http_put_response_hop_limit - (Optional) Desired HTTP PUT response hop limit for instance metadata requests. The larger the number, the further instance metadata requests can travel. Valid values are integer from 1 to 64. Defaults to 1. http_tokens - (Optional) Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2 (IMDSv2). Valid values include optional or required. Defaults to optional. instance_metadata_tags - (Optional) Enables or disables access to instance tags from the instance metadata service. Valid values include enabled or disabled. Defaults to disabled. |
object({ |
{ |
no |
instance_profile_data | (optional) IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile. | object({ |
{ |
no |
instance_type | Instance type for EC2 instance | string |
n/a | yes |
load_balancer_create_timeout | Timeout value when creating the ALB. | string |
"10m" |
no |
load_balancer_data | (optional) describe your variable | object({ |
{ |
no |
load_balancer_delete_timeout | Timeout value when deleting the ALB. | string |
"10m" |
no |
load_balancer_security_group_data | (optional) Security Group data for Loadbalancer | object({ |
{ |
no |
load_balancer_update_timeout | Timeout value when updating the ALB. | string |
"10m" |
no |
name | Name of the instance | string |
n/a | yes |
private_ip | (optional) Private IP for the instance | string |
null |
no |
root_block_device_data | The root_block_device block supports the following: delete_on_termination - (Optional) Whether the volume should be destroyed on instance termination. Defaults to true. encrypted - (Optional) Whether to enable volume encryption. Defaults to false. Must be configured to perform drift detection. iops - (Optional) Amount of provisioned IOPS. Only valid for volume_type of io1, io2 or gp3. kms_key_id - (Optional) Amazon Resource Name (ARN) of the KMS Key to use when encrypting the volume. Must be configured to perform drift detection. tags - (Optional) Map of tags to assign to the device. throughput - (Optional) Throughput to provision for a volume in mebibytes per second (MiB/s). This is only valid for volume_type of gp3. volume_size - (Optional) Size of the volume in gibibytes (GiB). volume_type - (Optional) Type of volume. Valid values include standard, gp2, gp3, io1, io2, sc1, or st1. Defaults to the volume type that the AMI uses. |
object({ |
n/a | yes |
security_group_data | (optional) Security Group data | object({ |
n/a | yes |
ssh_key_pair | (optional) SSH Key Pair for EC2 instance | string |
null |
no |
subnet_id | Subnet ID on which EC2 instance has to be created | string |
n/a | yes |
tags | (optional) Tags for EC2 instance | map(string) |
{} |
no |
target_groups | Target Group details | map(object({ |
{} |
no |
user_data | (optional) User data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user_data_base64 instead. Updates to this field will trigger a stop/start of the EC2 instance by default. | string |
null |
no |
user_data_base64 | (optional) Can 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 |
string |
null |
no |
vpc_id | The VPC ID where resources will be deployed. | string |
n/a | yes |
Outputs¶
Name | Description |
---|---|
arn | Instance ID |
id | Instance ID |
listener_arn | Listener ARN |
load_balancer_arn | n/a |
target_group_arn | Target Group ARN |
Git commits¶
while Contributing or doing git commit please specify the breaking change in your commit message whether its major,minor or patch
For Example
Development¶
Prerequisites¶
Configurations¶
- Configure pre-commit hooks
Tests¶
- Tests are available in
test
directory - Configure the dependencies
- Now execute the test
Authors¶
This project is authored by: - SourceFuse