Skip to content

Commit 01e4f70

Browse files
authored
Merge pull request #18 from DataDog/asg
Create EC2 instance in ASG
2 parents d8526af + 17ef045 commit 01e4f70

File tree

7 files changed

+82
-37
lines changed

7 files changed

+82
-37
lines changed

modules/instance/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,26 @@ No modules.
2020

2121
| Name | Type |
2222
|------|------|
23-
| [aws_instance.instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
23+
| [aws_autoscaling_group.asg](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group) | resource |
24+
| [aws_launch_template.launch_template](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
2425
| [aws_ami.al2023](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
2526

2627
## Inputs
2728

2829
| Name | Description | Type | Default | Required |
2930
|------|-------------|------|---------|:--------:|
30-
| <a name="input_availability_zone"></a> [availability\_zone](#input\_availability\_zone) | AZ to start the instance in | `string` | `null` | no |
31+
| <a name="input_asg_size"></a> [asg\_size](#input\_asg\_size) | Size of the autoscaling group the instance is in (i.e. number of instances to run) | `number` | `1` | no |
3132
| <a name="input_iam_instance_profile"></a> [iam\_instance\_profile](#input\_iam\_instance\_profile) | IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile | `string` | n/a | yes |
3233
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | The type of instance | `string` | `"t4g.large"` | no |
3334
| <a name="input_key_name"></a> [key\_name](#input\_key\_name) | Key name of the Key Pair to use for the instance; which can be managed using the `aws_key_pair` resource | `string` | `null` | no |
34-
| <a name="input_monitoring"></a> [monitoring](#input\_monitoring) | If true, the launched EC2 instance will have detailed monitoring enabled | `bool` | `null` | no |
35-
| <a name="input_name"></a> [name](#input\_name) | Name to be used on EC2 instance created | `string` | `"DatadogAgentlessScanner"` | no |
35+
| <a name="input_monitoring"></a> [monitoring](#input\_monitoring) | If true, the launched EC2 instance will have detailed monitoring enabled | `bool` | `false` | no |
36+
| <a name="input_name"></a> [name](#input\_name) | Name prefix to be used on EC2 instance created | `string` | `"DatadogAgentlessScanner"` | no |
3637
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | The VPC Subnet ID to launch in | `string` | n/a | yes |
3738
| <a name="input_tags"></a> [tags](#input\_tags) | A map of additional tags to add to the instance/volume created | `map(string)` | `{}` | no |
3839
| <a name="input_user_data"></a> [user\_data](#input\_user\_data) | The user data to provide when launching the instance | `string` | `null` | no |
3940
| <a name="input_vpc_security_group_ids"></a> [vpc\_security\_group\_ids](#input\_vpc\_security\_group\_ids) | A list of security group IDs to associate with | `list(string)` | `null` | no |
4041

4142
## Outputs
4243

43-
| Name | Description |
44-
|------|-------------|
45-
| <a name="output_instance"></a> [instance](#output\_instance) | The Datadog agentless scanner instance created |
44+
No outputs.
4645
<!-- END_TF_DOCS -->

modules/instance/main.tf

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,79 @@ data "aws_ami" "al2023" {
2222
}
2323
}
2424

25-
resource "aws_instance" "instance" {
26-
ami = data.aws_ami.al2023.id
27-
instance_type = var.instance_type
25+
resource "aws_launch_template" "launch_template" {
26+
name_prefix = "DatadogAgentlessScannerLaunchTemplate"
27+
image_id = data.aws_ami.al2023.id
28+
instance_type = var.instance_type
29+
user_data = base64encode(var.user_data)
30+
vpc_security_group_ids = var.vpc_security_group_ids
31+
key_name = var.key_name
2832

29-
user_data = var.user_data
30-
user_data_replace_on_change = true
33+
block_device_mappings {
34+
device_name = data.aws_ami.al2023.root_device_name
35+
ebs {
36+
encrypted = true
37+
}
38+
}
3139

32-
availability_zone = var.availability_zone
33-
subnet_id = var.subnet_id
34-
vpc_security_group_ids = var.vpc_security_group_ids
40+
monitoring {
41+
enabled = var.monitoring
42+
}
3543

36-
key_name = var.key_name
37-
iam_instance_profile = var.iam_instance_profile
38-
monitoring = var.monitoring
44+
iam_instance_profile {
45+
name = var.iam_instance_profile
46+
}
3947

4048
metadata_options {
4149
http_tokens = "required"
4250
}
4351

44-
root_block_device {
45-
encrypted = true
52+
# Tag created instances, volumes and network interface at launch
53+
dynamic "tag_specifications" {
54+
for_each = toset(["instance", "volume", "network-interface"])
55+
content {
56+
resource_type = tag_specifications.value
57+
tags = merge(
58+
var.tags,
59+
local.dd_tags,
60+
# add a Name tag for instances only
61+
tag_specifications.value == "instance" ? { "Name" = var.name } : {}
62+
)
63+
}
4664
}
4765

48-
tags = merge({ "Name" = var.name }, var.tags, local.dd_tags)
49-
volume_tags = merge({ "Name" = var.name }, var.tags, local.dd_tags)
66+
tags = merge(var.tags, local.dd_tags)
67+
68+
}
69+
70+
resource "aws_autoscaling_group" "asg" {
71+
name = "datadog-agentless-scanner-asg"
72+
min_size = var.asg_size
73+
max_size = var.asg_size
74+
desired_capacity = var.asg_size
75+
76+
vpc_zone_identifier = [var.subnet_id]
77+
78+
launch_template {
79+
id = aws_launch_template.launch_template.id
80+
version = aws_launch_template.launch_template.latest_version
81+
}
82+
83+
instance_refresh {
84+
strategy = "Rolling"
85+
preferences {
86+
# Whenever the launch template changes, allow replacing instances all at once
87+
min_healthy_percentage = 0
88+
}
89+
}
90+
91+
# aws_autoscaling_group doesn't have a "tags" attribute, but instead a "tag" block
92+
dynamic "tag" {
93+
for_each = merge({ "Name" = "DatadogAgentlessScannerASG" }, var.tags, local.dd_tags)
94+
content {
95+
key = tag.key
96+
value = tag.value
97+
propagate_at_launch = false # tagging is handled by the launch template, here we only tag the ASG itself
98+
}
99+
}
50100
}

modules/instance/outputs.tf

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
output "instance" {
2-
description = "The Datadog agentless scanner instance created"
3-
value = aws_instance.instance
4-
}
1+
# No outputs for now

modules/instance/variables.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
variable "name" {
2-
description = "Name to be used on EC2 instance created"
2+
description = "Name prefix to be used on EC2 instance created"
33
type = string
44
default = "DatadogAgentlessScanner"
55
}
@@ -21,12 +21,6 @@ variable "subnet_id" {
2121
type = string
2222
}
2323

24-
variable "availability_zone" {
25-
description = "AZ to start the instance in"
26-
type = string
27-
default = null
28-
}
29-
3024
variable "vpc_security_group_ids" {
3125
description = "A list of security group IDs to associate with"
3226
type = list(string)
@@ -47,7 +41,13 @@ variable "key_name" {
4741
variable "monitoring" {
4842
description = "If true, the launched EC2 instance will have detailed monitoring enabled"
4943
type = bool
50-
default = null
44+
default = false
45+
}
46+
47+
variable "asg_size" {
48+
description = "Size of the autoscaling group the instance is in (i.e. number of instances to run)"
49+
type = number
50+
default = 1
5151
}
5252

5353
variable "tags" {

modules/user_data/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ No modules.
2929
| <a name="input_api_key"></a> [api\_key](#input\_api\_key) | Specifies the API key required by the Datadog Agent to submit vulnerabilities to Datadog | `string` | `null` | no |
3030
| <a name="input_api_key_secret_arn"></a> [api\_key\_secret\_arn](#input\_api\_key\_secret\_arn) | ARN of the secret holding the Datadog API key. Takes precedence over api\_key variable | `string` | `null` | no |
3131
| <a name="input_hostname"></a> [hostname](#input\_hostname) | Specifies the hostname the agentless-scanning agent will report as | `string` | n/a | yes |
32-
| <a name="input_scanner_version"></a> [scanner\_version](#input\_scanner\_version) | Specifies the agentless scanner version installed | `string` | `"50.0~rc.7~agentless~scanner~2023121801"` | no |
32+
| <a name="input_scanner_version"></a> [scanner\_version](#input\_scanner\_version) | Specifies the agentless scanner version installed | `string` | n/a | yes |
3333
| <a name="input_site"></a> [site](#input\_site) | By default the Agent sends its data to Datadog US site. If your organization is on another site, you must update it. See https://docs.datadoghq.com/getting_started/site/ | `string` | `"datadoghq.com"` | no |
3434

3535
## Outputs

modules/user_data/variables.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ variable "site" {
2626
variable "scanner_version" {
2727
description = "Specifies the agentless scanner version installed"
2828
type = string
29-
default = "50.0~rc.7~agentless~scanner~2023121801"
3029
nullable = false
3130
}
3231

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ variable "site" {
2020
variable "scanner_version" {
2121
description = "Specifies the agentless scanner version installed"
2222
type = string
23-
default = null
23+
default = "50.0~rc.7~agentless~scanner~2023121801"
2424
}
2525

2626
variable "instance_profile_name" {

0 commit comments

Comments
 (0)