EC2 instance user data is not executed.

by XDK 18. September 2019 22:27
Explanation:
 
The user data is not executed due to the leading whitespaces in the following format
 
resource "aws_instance" "Terraform-Demo" {
  ami = "${lookup(var.RegionToAmazonAMI, var.Region)}"
  instance_type = "t2.micro"
  key_name = "myaccountkey"
  vpc_security_group_ids =  ["${aws_security_group.webserver-sg-2.id}"]
  user_data = <<EOF
    #cloud-boothook
    #! /bin/bash
    sudo apt update
    sudo apt install -y apache2
    IP_ADDR=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
    sudo chmod 777  /var/www/html
    sudo echo "Terraform instance 2 with IP IP_ADDR" > /var/www/html/index.html
    sudo echo "Terraform instance 2 with IP IP_ADDR -- OK" > /var/www/html/health.html
  EOF
  provisioner "local-exec" {
    command = "echo ${aws_instance.Terraform-Demo.public_ip} > testP.txt"
    }
  tags = {
    Name = "TerraformDemo"
  }
}
 
Solution:
 
Either add hyphen in <<-EOF to trim the leading whitespaces or remove the whitespaces manually 

Tags:

Amazon Web Service | Terraform

EC2 instances launched in the subnet missing public IPv4 address

by XDK 18. September 2019 22:09

Explanation:
EC2 instances launched in the subnet using Terraform or Cloudformation is missing public IPv4 address

Solution:
Add the following argument/property

CloudFormation:

MapPublicIpOnLaunch - Indicates whether instances launched in this subnet receive a public IPv4 address.

PubSubnetZoneA:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone:
Fn::Select:
- '0'
- Fn::GetAZs:
Ref: 'AWS::Region'
CidrBlock: '10.0.10.0/24'
MapPublicIpOnLaunch: 'True'
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Public', 'Zone A', !Ref 'AWS::StackName' ] ]

Terraform:

map_public_ip_on_launch - (Optional) Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.

resource "aws_subnet" "public-subnet-a" {
vpc_id = "${aws_vpc.vpc.id}"
map_public_ip_on_launch = true
cidr_block = "10.0.10.0/24"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
tags = {
Name = "public-subnet-a:${var.labname}"
}
}

Tags:

Amazon Web Service | Terraform | CloudFormation

A data resource "aws_availability_zones" "available" has not been declared in the root module.

by XDK 18. September 2019 21:45

Exception:

Error: Reference to undeclared resource
on base.tf line 65, in resource "aws_subnet" "public-subnet-a":
65: availability_zone = "${data.aws_availability_zones.available.names[0]}"
A data resource "aws_availability_zones" "available" has not been declared in
the root module.

Explanation:

The exception occurred while assigning the availability zone for a region in the terraform template.

resource "aws_subnet" "public-subnet-a" {
  vpc_id = "${aws_vpc.vpc.id}"
  map_public_ip_on_launch = true
  cidr_block = "10.0.10.0/24"
  availability_zone = "${data.aws_availability_zones.available.names[0]}"
  tags = {
  Name = "public-subnet-a:${var.labname}"
  }
}

Solution:

Add data module to get the availability zones for a region
data "aws_availability_zones" "available" {}

resource "aws_subnet" "public-subnet-a" {
  vpc_id = "${aws_vpc.vpc.id}"
  map_public_ip_on_launch = true
  cidr_block = "10.0.10.0/24"
  availability_zone = "${data.aws_availability_zones.available.names[0]}"
  tags = {
  Name = "public-subnet-a:${var.labname}"
  }
}

Tags:

Amazon Web Service | Terraform

About the author

My name is Xavier Dilip Kumar Jayaraj having 16+ years of IT experience which includes solid experience and depth Knowledge in Application Life Cycle Management, Configuration Management, Implementation and Support using TFS on-premises and Azure DevOps. I have invested in gaining DevOps knowledege to expertise with Cloud Computing providers namely Microsoft Azure and Amazon Web Services in recent years. I am very positive to learn and adapt emerging technologies to client’s environment.

Microsoft Certified: Azure Administrator Associate

Microsoft Certified: Azure DevOps Engineer Expert

DevOps Engineer Certificate Program | Transcript 

OTP-AWSD5: AWS IoT: Developing and Deploying an Internet of Things

[PCEP-30-01] PCEP – Certified Entry-Level Python Programmer

Quotes I Like

"Failure will never overtake me if my determination to succeed is strong enough."  - Dr. APJ. Abdul Kalam

"Always be yourself, express yourself, have faith in yourself, do not go out and look for a successful personality and duplicate it." - Bruce Lee

"Technology is just a tool. In terms of getting the kids working together and motivating them, the teacher is the most important." - Bill Gates

"Innovation distinguishes between a leader and a follower." - Steve Jobs

CategoryList

Disclaimer

The information provided here is based on my expreriences, troubleshooting and online/offline findings. It can be used as is on your own risk without any warranties and I impose no rights.