/provisioning-instances

Provisioning instance on aws with terraform

Primary LanguageHCL

Provisioning ec2 using Terraform - AWS

Requirements

Name Version
terraform >= 1.2.9
aws >= 5.72.0

Providers

Name Version
aws >= 5.72.0
tls n/a

Used localstack

pip install localstack
localstack start -d
localstack status services
aws configure --profile local
AWS Access Key ID [****************ocal]: local
AWS Secret Access Key [****************ocal]: local
Default region name [us-east-1]: 
Default output format [json]: 

Para mais informações segue repo localstack: https://github.com/localstack/localstack

Command aws-cli

export AWS_PROFILE=local
aws ec2 --endpoint-url=http://localhost:4566 describe-instances --filters Name=tag-key,Values=Name --query 'Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value}'

For used script in python instances.py required boto3

pip3 install boto3

## provider

provider "aws" {
  region                      = var.region
  profile                     = var.profile

  ## necessário para uso com localstack
  skip_credentials_validation = true 
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints {
    iam        = "http://localhost:4566"
    ec2        = "http://localhost:4566"

  }
}

Example execute module

module "ec2" {
  source = "github.com/Emerson89/provisioning-instances.git//?ref=master"

  name                        = "ec2-terraform"
  instance_type               = "t3.micro"
  associate_public_ip_address = false
  key_name                    = "key"
  eip                         = false
  subnet_id                   = "subnet-abcabcabcabc"
  image_name                  = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"
  owner                       = "099720109477"

  additional_rules_security_group = {

    ingress_rule_1 = {
      from_port   = 22
      to_port     = 22
      protocol    = "tcp"
      cidr_blocks = ["172.16.3.10/32"]
      description = "SSH"
      type        = "ingress"
    },
  }

  additional_policy           = true

  policy_additional = [
    {
      name = "policy-test"
      policy = jsonencode({
        Version = "2012-10-17",
        Statement = [
          {
            Effect = "Allow",
            Action = [
              "s3:PutObject",
              "s3:GetObject",
              "s3:DeleteObject",
              "s3:ListMultipartUploadParts",
              "s3:AbortMultipartUpload",
            ],
            Resource = [
              "arn:aws:s3:::test1234567678/*"
            ],
          },
        ],
      })
    }
  ]

  root_block_device = [
    {
      volume_type = "gp3"
      volume_size = 10
      #delete_on_termination = false
      tags = {
        Name = "root-block"
      }
    },
  ]
  tags = {
    Environment = "Development"
  }
}

Resources

Name Type
aws_eip.this resource
aws_iam_instance_profile.this resource
aws_iam_role.this resource
aws_iam_role_policy.this resource
aws_iam_role_policy_attachment.this resource
aws_instance.this resource
aws_key_pair.this resource
aws_security_group.this resource
aws_security_group_rule.this resource
tls_private_key.this resource
aws_ami.img data source
aws_availability_zones.azs data source
aws_subnet.this data source
aws_vpc.this data source

Inputs

Name Description Type Default Required
additional_policy create policy bool false no
additional_rules_security_group Rules security group any {} no
ami n/a string "" no
associate_public_ip_address Whether to associate a public IP address with an instance in a VPC bool false no
azs AZ to start the instance in string null no
cpu_credits The credit option for CPU usage (unlimited or standard) string null no
create_instance If true, the launched EC2 instance will have detailed monitoring enabled bool true no
disable_api_termination If true, enables EC2 Instance Termination Protection bool null no
ebs_block_device Additional EBS block devices to attach to the instance list(map(string)) [] no
ebs_optimized If true, the launched EC2 instance will be EBS-optimized bool null no
eip Enabled eip bool false no
enable_volume_tags Whether to enable volume tags (if enabled it conflicts with root_block_device tags) bool false no
ephemeral_block_device Customize Ephemeral (also known as Instance Store) volumes on the instance list(map(string)) [] no
image_name Values datasource ami string "ubuntu/*" no
instance_count Número de instâncias que serão provisionadas number 1 no
instance_type The type of instance to start string "t3.micro" no
key_name Key name of the Key Pair to use for the instance; which can be managed using the aws_key_pair resource string "key" no
launch_template Specifies a Launch Template to configure the instance. Parameters configured on this resource will override the corresponding parameters in the Launch Template map(string) null no
monitoring If true, the launched EC2 instance will have detailed monitoring enabled bool false no
name Name to be used on EC2 instance created string "ec2-by-terraform" no
network_interface Customize network interfaces to be attached at instance boot time list(map(string)) [] no
owner Owner ami any "amazon" no
policy_additional additional policy list(any) [] no
private_ip Private IP address to associate with the instance in a VPC string null no
root_block_device Customize details about the root block device of the instance. See Block Devices below for details list(any) [] no
subnet_id The VPC Subnet ID to launch in string "" no
tags A mapping of tags to assign to the resource map(string) {} no
timeouts Define maximum timeout for creating, updating, and deleting EC2 instance resources map(string) {} no
use_data_ami n/a bool false no
user_data The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user_data_base64 instead. string null no
user_data_base64 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
volume_tags A mapping of tags to assign to the devices created by the instance at launch time map(string) {} no
vpc_security_group_ids A list of security group IDs to associate with list(string) null no

Outputs

Name Description
arn The ARN of the instance
az n/a
id The ID of the instance
instance_state The state of the instance. One of: pending, running, shutting-down, terminated, stopping, stopped
outpost_arn The ARN of the Outpost the instance is assigned to
primary_network_interface_id The ID of the instance's primary network interface
private_dns The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC
private_ip The private IP address assigned to the instance.
public_dns The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC
public_ip The public IP address assigned to the instance, if applicable. NOTE: If you are using an aws_eip with your instance, you should refer to the EIP's address directly and not use public_ip as this field will change after the EIP is attached
tags_all A map of tags assigned to the resource, including those inherited from the provider default_tags configuration block