Lab8: Data Source - OT-TRAINING/TerraformAWS GitHub Wiki

Data Source

Lab: In this lab we will be working with Data Sources:

  • First we need to fetch the required VPC that already exist in our AWS Account.
  • Now use the ID of that VPC to create a Subnet.
  • Now Manually create one AMI image where you have installed Nginx on it.
  • Now create new Instance using TF where you have to make sure that you are using AMI created by you and it has to MOST Recent.
data "aws_vpc" "main" {
    default = true
}
resource "aws_subnet" "main" {
  vpc_id     = data.aws_vpc.main.id
  cidr_block = "172.31.64.0/20"

  tags = {
    Name = "test"
  }
}
data "aws_ami" "main" {
 
 most_recent = true


  filter {
    name   = "name"
    values = ["test-ami"]
  }
 
  owners = ["self"]
}



resource "aws_instance" "main" {
  ami           = data.aws_ami.main.id
  instance_type = var.instance
  
  tags = {
    Name = var.tags
 }
}