Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AWS] EC2 instance launch template #96

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions internal/plan/json_getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,12 @@ func getValue(key string, context *tfContext) (*valueWithUnit, error) {
unit := propertyMapping.Unit

for _, pathRaw := range paths {
if valueFound != nil {
if valueFound != nil && valueFound != ".not_found" {
break
}
path := pathRaw
if strings.Contains(pathRaw, "${") {
path, err = resolvePlaceholders(path, context)

if err != nil {
return nil, errors.Wrapf(err, "Cannot resolve placeholders for %v", path)
}
Expand Down Expand Up @@ -408,7 +407,7 @@ func getVariable(name string, contextParam *tfContext) (interface{}, error) {
if variablesMappings == nil {
context = contextParam.RootContext
if context != nil {
variablesMappings = context.Mapping.Variables
variablesMappings = context.RootContext.Mapping.Variables
}
}
if variablesMappings == nil {
Expand Down
45 changes: 37 additions & 8 deletions internal/plan/mappings/aws/ec2_instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,27 @@ compute_resource:
- '.configuration'
property: "region"
instance_type:
- paths: '.values.instance_type'
- paths:
- '.values.instance_type'
- '${launch_template}.values.instance_type'
launch_template:
- paths:
- '.configuration.root_module.resources[] | select(.address == "${this.address}") | .expressions.launch_template[]?.id?.references[]? | select(endswith(".id") or endswith(".name")) | gsub("\\.(id|name)$"; "")'
reference:
paths:
- cbf::all_select("address"; "${key}")
- cbf::all_select("address"; ("${key}" | split(".")[0:2] | join("."))) | .resources[] | select(.name", ("${key}" | split(".")[2]))
- .prior_state.values.root_module.resources[] | select(.address == "${key}")
return_path: true
ami:
- paths:
- '"${this.values.ami}"'
- '${launch_template}.values.image_id'
reference:
paths:
- cbf::all_select("type"; "aws_ami") | select(.type == "aws_ami") | select(.values.image_id == "${key}")
- .prior_state.values.root_module.resources[] | select(.type == "aws_ami") | select(.values.image_id == "${key}")
return_path: true
properties:
name:
- paths: ".name"
Expand All @@ -19,12 +39,14 @@ compute_resource:
type:
- paths: ".type"
vCPUs:
- paths: ".values.instance_type"
- paths:
- '"${instance_type}"'
reference:
json_file: aws_instances
property: ".VCPU"
memory:
- paths: ".values.instance_type"
- paths:
- '"${instance_type}"'
unit: mb
reference:
json_file: aws_instances
Expand All @@ -42,7 +64,8 @@ compute_resource:
storage:
- type: list
item:
- paths: '.prior_state.values.root_module.resources[] | select(.values.image_id == "${this.values.ami}") | .values.block_device_mappings[].ebs | select(length > 0)'
- paths:
- '${ami}.values.block_device_mappings[] | select(length > 0) | .ebs'
properties:
size:
- paths: ".volume_size"
Expand All @@ -53,7 +76,9 @@ compute_resource:
default: standard
reference:
general: disk_types
- paths: '.values.ebs_block_device[] | select(length > 0)'
- paths:
- '.values.ebs_block_device[] | select(length > 0)'
- '${launch_template}.values.block_device_mappings[] | select(length > 0) | select(.virtual_name == null or .virtual_name == "" or (.virtual_name | startswith("ephemeral") | not)) | .ebs'
properties:
size:
- paths: ".volume_size"
Expand All @@ -69,16 +94,20 @@ compute_resource:
default: standard
reference:
general: disk_types
- paths: '.values.ephemeral_block_device[] | select(length > 0)'
- paths:
- '.values.ephemeral_block_device[] | select(length > 0)'
- '${launch_template}.values.block_device_mappings[] | select(length > 0) | select(.virtual_name != null and (.virtual_name | startswith("ephemeral")))'
properties:
size:
- paths: '"${instance_type}"'
- paths:
- '"${instance_type}"'
unit: gb
reference:
json_file: aws_instances
property: ".InstanceStorage.SizePerDiskGB"
type:
- paths: '"${instance_type}"'
- paths:
- '"${instance_type}"'
default: standard
reference:
json_file: aws_instances
Expand Down
3 changes: 2 additions & 1 deletion internal/plan/mappings/aws/general.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ general:
ignored_resources:
- "aws_vpc"
- "aws_volume_attachment"
- "aws_launch_configuration"
- "aws_launch_configuration"
- "aws_launch_template"
18 changes: 18 additions & 0 deletions internal/plan/test/resources_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ func TestGetResource_EC2(t *testing.T) {
Count: 1,
},
},
"aws_instance.ec2_with_lt": resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "aws_instance.ec2_with_lt",
Name: "ec2_with_lt",
ResourceType: "aws_instance",
Provider: providers.AWS,
Region: "eu-west-3",
Count: 1,
ReplicationFactor: 1,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: int32(4),
MemoryMb: int32(16384),

HddStorage: decimal.NewFromInt(300),
SsdStorage: decimal.NewFromInt(30 + 150),
},
},
}
tfPlan, err := terraform.TerraformPlan()
assert.NoError(t, err)
Expand Down
28 changes: 28 additions & 0 deletions test/terraform/aws_ec2/ec2_launch_template.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
resource "aws_launch_template" "my_launch_template" {
name_prefix = "my_launch_template"
image_id = "${data.aws_ami.ubuntu.id}"
instance_type = "m5d.xlarge"

block_device_mappings {
device_name = "/dev/sda1"

ebs {
volume_size = 300
volume_type = "standard"
delete_on_termination = true
}
}

block_device_mappings {
device_name = "/dev/sdb"
virtual_name = "ephemeral0"
}

}

resource "aws_instance" "ec2_with_lt" {
launch_template {
id = "${aws_launch_template.my_launch_template.id}"
version = "$$Latest"
}
}
Loading