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

adds sync_compliance attribute to ssm_association resource, fixes #22945 #23515

Merged
merged 6 commits into from
Sep 1, 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
3 changes: 3 additions & 0 deletions .changelog/23515.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_ssm_association: Add `sync_compliance` attribute
```
18 changes: 16 additions & 2 deletions internal/service/ssm/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func ResourceAssociation() *schema.Resource {
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 256),
},
"sync_compliance": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ssm.AssociationSyncCompliance_Values(), false),
},
"output_location": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -186,12 +191,16 @@ func resourceAssociationCreate(ctx context.Context, d *schema.ResourceData, meta
associationInput.DocumentVersion = aws.String(v.(string))
}

if v, ok := d.GetOk("parameters"); ok {
associationInput.Parameters = expandDocumentParameters(v.(map[string]interface{}))
}

if v, ok := d.GetOk("schedule_expression"); ok {
associationInput.ScheduleExpression = aws.String(v.(string))
}

if v, ok := d.GetOk("parameters"); ok {
associationInput.Parameters = expandDocumentParameters(v.(map[string]interface{}))
if v, ok := d.GetOk("sync_compliance"); ok {
associationInput.SyncCompliance = aws.String(v.(string))
}

if v, ok := d.GetOk("targets"); ok {
Expand Down Expand Up @@ -270,6 +279,7 @@ func resourceAssociationRead(ctx context.Context, d *schema.ResourceData, meta i
d.Set("name", association.Name)
d.Set("association_id", association.AssociationId)
d.Set("schedule_expression", association.ScheduleExpression)
d.Set("sync_compliance", association.SyncCompliance)
d.Set("document_version", association.DocumentVersion)
d.Set("compliance_severity", association.ComplianceSeverity)
d.Set("max_concurrency", association.MaxConcurrency)
Expand Down Expand Up @@ -318,6 +328,10 @@ func resourceAssociationUpdate(ctx context.Context, d *schema.ResourceData, meta
associationInput.ScheduleExpression = aws.String(v.(string))
}

if d.HasChange("sync_compliance") {
associationInput.SyncCompliance = aws.String(d.Get("sync_compliance").(string))
}

if v, ok := d.GetOk("parameters"); ok {
associationInput.Parameters = expandDocumentParameters(v.(map[string]interface{}))
}
Expand Down
53 changes: 53 additions & 0 deletions internal/service/ssm/association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,35 @@ func TestAccSSMAssociation_rateControl(t *testing.T) {
})
}

func TestAccSSMAssociation_syncCompliance(t *testing.T) {
ctx := acctest.Context(t)
rName := "AWS-RunPatchBaselineAssociation"
resourceName := "aws_ssm_association.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ssm.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAssociationDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAssociationSyncComplianceConfig(rName, "MANUAL"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAssociationExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "sync_compliance", "MANUAL"),
),
},
{
Config: testAccAssociationSyncComplianceConfig(rName, "AUTO"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAssociationExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "sync_compliance", "AUTO"),
),
},
},
})
}

func testAccCheckAssociationExists(ctx context.Context, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -1572,6 +1601,30 @@ resource "aws_ssm_association" "test" {
`, rName, rate)
}

func testAccAssociationSyncComplianceConfig(rName, syncCompliance string) string {
return fmt.Sprintf(`
resource "aws_ssm_association" "test" {
name = %[1]q
targets {
key = "InstanceIds"
values = ["*"]
}
apply_only_at_cron_interval = false
sync_compliance = %[2]q
parameters = {
Operation = "Scan"
RebootOption = "NoReboot"
}
schedule_expression = "cron(0 6 ? * * *)"
lifecycle {
ignore_changes = [
parameters["AssociationId"]
]
}
}
`, rName, syncCompliance)
}

func testAccAssociationConfig_outputLocationAndWaitForSuccess(rName string) string {
return acctest.ConfigCompose(
testAccAssociationWithOutputLocationS3RegionConfigBase(rName),
Expand Down
9 changes: 5 additions & 4 deletions website/docs/r/ssm_association.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ This resource supports the following arguments:
* `name` - (Required) The name of the SSM document to apply.
* `apply_only_at_cron_interval` - (Optional) By default, when you create a new or update associations, the system runs it immediately and then according to the schedule you specified. Enable this option if you do not want an association to run immediately after you create or update it. This parameter is not supported for rate expressions. Default: `false`.
* `association_name` - (Optional) The descriptive name for the association.
* `automation_target_parameter_name` - (Optional) Specify the target for the association. This target is required for associations that use an `Automation` document and target resources by using rate controls. This should be set to the SSM document `parameter` that will define how your automation will branch out.
* `compliance_severity` - (Optional) The compliance severity for the association. Can be one of the following: `UNSPECIFIED`, `LOW`, `MEDIUM`, `HIGH` or `CRITICAL`
* `document_version` - (Optional) The document version you want to associate with the target(s). Can be a specific version or the default version.
* `instance_id` - (Optional, **Deprecated**) The instance ID to apply an SSM document to. Use `targets` with key `InstanceIds` for document schema versions 2.0 and above. Use the `targets` attribute instead.
* `max_concurrency` - (Optional) The maximum number of targets allowed to run the association at the same time. You can specify a number, for example 10, or a percentage of the target set, for example 10%.
* `max_errors` - (Optional) The number of errors that are allowed before the system stops sending requests to run the association on additional targets. You can specify a number, for example 10, or a percentage of the target set, for example 10%. If you specify a threshold of 3, the stop command is sent when the fourth error is returned. If you specify a threshold of 10% for 50 associations, the stop command is sent when the sixth error is returned.
* `output_location` - (Optional) An output location block. Output Location is documented below.
* `parameters` - (Optional) A block of arbitrary string parameters to pass to the SSM document.
* `schedule_expression` - (Optional) A [cron or rate expression](https://docs.aws.amazon.com/systems-manager/latest/userguide/reference-cron-and-rate-expressions.html) that specifies when the association runs.
* `sync_compliance` - (Optional) The mode for generating association compliance. You can specify `AUTO` or `MANUAL`.
* `targets` - (Optional) A block containing the targets of the SSM association. Targets are documented below. AWS currently supports a maximum of 5 targets.
* `compliance_severity` - (Optional) The compliance severity for the association. Can be one of the following: `UNSPECIFIED`, `LOW`, `MEDIUM`, `HIGH` or `CRITICAL`
* `max_concurrency` - (Optional) The maximum number of targets allowed to run the association at the same time. You can specify a number, for example 10, or a percentage of the target set, for example 10%.
* `max_errors` - (Optional) The number of errors that are allowed before the system stops sending requests to run the association on additional targets. You can specify a number, for example 10, or a percentage of the target set, for example 10%. If you specify a threshold of 3, the stop command is sent when the fourth error is returned. If you specify a threshold of 10% for 50 associations, the stop command is sent when the sixth error is returned.
* `automation_target_parameter_name` - (Optional) Specify the target for the association. This target is required for associations that use an `Automation` document and target resources by using rate controls. This should be set to the SSM document `parameter` that will define how your automation will branch out.
* `wait_for_success_timeout_seconds` - (Optional) The number of seconds to wait for the association status to be `Success`. If `Success` status is not reached within the given time, create opration will fail.

Output Location (`output_location`) is an S3 bucket where you want to store the results of this association:
Expand Down
Loading