How to add a parameter to AWS Terraform
This guide details how to expand the existing Terraform setup to include a new configuration parameter: jit_capacity. This parameter allows you to control the maximum number of concurrent JIT streams allowed in your environment. See Services - Orchestrator for available environment variables.
To achieve this, you will propagate a new variable from your Terraform inputs down to the user_data script running on the Orchestrator instance.
Prerequisites
- You have followed the instructions in How to run the JIT in a multi instance AWS environment to get started with a multi instance setup
Define the Input Variable
Navigate to the terraform/aws folder in the jit-deployment-examples repository.
Begin with declaring the new variable so that Terraform can accept it as an input.
Open variables.tf. This file currently holds variables such as vpc_id and target_version. Append the following block to define jit_capacity as a number with a default value (e.g., 10), allowing you to override it at runtime if necessary.
variable "jit_capacity" {
description = "Max number of concurrent JIT streams"
type = number
default = 10
}
Pass the Variable to the Orchestrator
Next, you need to pass this variable into the Orchestrator's configuration template. The Orchestrator resource is defined in compute.tf, which uses a templatefile function to generate the user data script.
Locate the aws_instance resource named orchestrator in compute.tf. Inside the user_data block, add the jit_capacity key to the variable map. This map already contains variables like jit_ami_id and jit_orchestrator_port.
resource "aws_instance" "orchestrator" {
# ... existing configuration ...
user_data = templatefile("${path.module}/user_data.yaml.tpl", {
jit_ami_id = data.aws_ami.agent_image.id
jit_agent_profile = aws_iam_instance_profile.agent_profile.name
jit_security_group_id = aws_security_group.agent_sg.id
jit_key_name = aws_key_pair.jit_key.key_name
jit_orchestrator_port = var.orchestrator_port
jit_instance_agent_port = var.instance_agent_port
jit_capacity = var.jit_capacity
})
# ... existing configuration ...
}
Update the User Data Template
Finally, you must update the template file to write this value into the environment file on the server. The user_data.yaml.tpl file currently writes configuration to /opt/jit/jit.env.
Open user_data.yaml.tpl and append the new environment variable JIT_CAPACITY to the content block.
#cloud-config
write_files:
- path: /opt/jit/jit.env
content: |
JIT_AWS_AMI_ID=${jit_ami_id}
JIT_AWS_AGENT_INSTANCE_PROFILE_NAME=${jit_agent_profile}
JIT_AWS_SECURITY_GROUP_ID=${jit_security_group_id}
JIT_AWS_KEY_PAIR_NAME=${jit_key_name}
JIT_ORCHESTRATOR_PORT=${jit_orchestrator_port}
JIT_INSTANCE_AGENT_PORT_MIN=${jit_instance_agent_port}
JIT_INSTANCE_AGENT_PORT_MAX=${jit_instance_agent_port}
JIT_CAPACITY=${jit_capacity}
append: true
owner: ubuntu:ubuntu
Verification
To verify the changes, initialize and apply the Terraform configuration. You can pass a custom value for the new parameter to ensure it is being picked up correctly.
terraform apply -var "jit_capacity=20" -var "target_version=<insert version here>"
After the Orchestrator instance provisions, you can SSH into the instance and check the contents of the environment file:
cat /opt/jit/jit.env
You should see JIT_CAPACITY=20 (or your default value) at the bottom of the file.