Passing variables between Terragrunt and Terraform
I am trying to create an EC2 instance with an EBS volume attached to the said instance. I have the code to create the EC2 instance using terragrunt, and it works fine. However, to create the EBS volume and attach it to the instance I need to use some terraform code. e.g. Layout tree is: dev -ec2 --terragrunt.hcl --ebs.tf In the ebs.tf file we can have > resource "aws_ebs_volume" "this" { > availability_zone = "ap-southeast-2a" > size = 20 > } > > resource "aws_volume_attachment" "this" { > device_name = "/dev/sdh" > volume_id = aws_ebs_volume.this.id > instance_id = <instance.parameter.from.terragrunt> > } > terragrunt.hcl > locals { > environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl")) > env = local.environment_vars.locals.environment > > project_vars = read_terragrunt_config(find_in_parent_folders("project.hcl")) > project = local.project_vars.locals.project_name > application = local.project_vars.locals.application_name > > } > > include { > path = find_in_parent_folders() > } > > terraform { > source = "git::git@github.com:terraform-aws-modules/terraform-aws-ec2-instance.git?ref=v3.3.0" > } > `` > dependency "sg" { > config_path = "../sg-ec2" > > mock_outputs = { > security_group_id = "sg-xxxxxxxxxxxx" > } > } > > inputs = { > > > name = "ui01-${local.project}-${local.application}-${local.env}" > description = "UI 01 ${local.project} ${local.application} Instance for ${local.env}" > > > ami = "ami-0bd2230cfb28832f7" # Amazon Linux kernel 5.10 > instance_type = "c5.large" > key_name = "key-test" # This key is manually created > monitoring = true > iam_instance_profile = "AmazonSSMRoleForInstancesQuickSetup" > > > vpc_id = "vpc-xxxxxxx" > subnet_id = "subnet-xxxxxxxx" > > > vpc_security_group_ids = ["${dependency.sg.outputs.security_group_id}"] > > } Is it possible to use the output of the instance and pass this parameter/object to the ebs.tf file so that the ebs volume gets attached to the instance on the fly? Another question is, is it possible for the *.tf files to use the variables defined in the .hcl files? e.g. If you call in terragrunt > locals { > environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl")) > env = local.environment_vars.locals.environment > } > > env.hcl is: > locals { > environment = "dev" > } > you can use the variable env as ${local.env} for your inputs Can you call this variable in the .tf file in some way?
OK so I have this almost working fully, well in fact it does work, I can grab the instance id and attach an ebs volume to this instance, but at the same time the ebs directory tries to create a new ec2 instance. This is not what I want as I have a ec2 directory looking after the entire ec2 instance creation. ├── ebs │ ├── ebs.tf │ └── terragrunt.hcl └── ec2-instance └── terragrunt.hcl ebs.tf ``` variable "instance_id" { type = string } resource "aws_ebs_volume" "this" { availability_zone = "ap-southeast-2a" size = 20 } resource "aws_volume_attachment" "this" { device_name = "/dev/sdh" volume_id = aws_ebs_volume.this.id instance_id = "${var.instance_id}" } ``` terragrunt.hcl ``` locals { } include { path = find_in_parent_folders() } terraform { source = "git::git@github.com:terraform-aws-modules/terraform-aws-ec2-instance.git?ref=v3.3.0" } dependency "ec2-linux-ui" { config_path = "../ec2-linux-ui" mock_outputs = { instance_id = "12345" } } inputs = { instance_id = dependency.ec2-linux-ui.outputs.id } ``` terragrunt.hcl for the ec2 instance ``` locals { environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl")) env = local.environment_vars.locals.environment project_vars = read_terragrunt_config(find_in_parent_folders("project.hcl")) project = local.project_vars.locals.project_name application = local.project_vars.locals.application_name } include { path = find_in_parent_folders() } terraform { source = "git::git@github.com:terraform-aws-modules/terraform-aws-ec2-instance.git?ref=v3.3.0" } # Need the output of the correct Security Group ID to attach to the RDS instance dependency "sg" { config_path = "../sg-ec2" mock_outputs = { security_group_id = "sg-xxxxxxxxxx" } } inputs = { # Naming name = "ui01-${local.project}-${local.application}-${local.env}" description = "UI 01 ${local.project} ${local.application} Instance for ${local.env}" # EC2 Config ami = "ami-0bd2230cfb28832f7" # Amazon Linux kernel 5.10 instance_type = "c5.large" key_name = "xxxxxxx" monitoring = true # Networking vpc_id = "xxxxxxx" subnet_id = "xxxxxxxx" # Security Group vpc_security_group_ids = ["${dependency.sg.outputs.security_group_id}"] } ``` Not sure why the ebs/terragrunt.hcl file wants to create a new instance when I can successfully get the instance id returned from the ec2-linux-ui dependency? If I can fix that, we are done.