Skip to content

Commit

Permalink
resource/aws_cloudformation_stack: Fix perpetual diff on template wit…
Browse files Browse the repository at this point in the history
…h transform (#9006)

In refreshing the state of an aws_cloudformation_stack, request the "Original" template body
with CFn GetTemplate API to avoid perpetual diff in case the template uses macro transform,
such as the AWS::Serverless framework.

ref: https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_GetTemplate.html

Output from acceptance testing:

```
--- PASS: TestAccAWSCloudFormationStack_allAttributes (66.95s)
--- PASS: TestAccAWSCloudFormationStack_basic (65.97s)
--- PASS: TestAccAWSCloudFormationStack_dataSource_basic (65.60s)
--- PASS: TestAccAWSCloudFormationStack_dataSource_yaml (68.56s)
--- PASS: TestAccAWSCloudFormationStack_defaultParams (65.19s)
--- PASS: TestAccAWSCloudFormationStack_disappears (66.83s)
--- PASS: TestAccAWSCloudFormationStack_withParams (114.62s)
--- PASS: TestAccAWSCloudFormationStack_withTransform (43.79s)
--- PASS: TestAccAWSCloudFormationStack_withUrl_withParams (116.98s)
--- PASS: TestAccAWSCloudFormationStack_withUrl_withParams_noUpdate (71.85s)
--- PASS: TestAccAWSCloudFormationStack_withUrl_withParams_withYaml (64.05s)
--- PASS: TestAccAWSCloudFormationStack_yaml (64.69s)
```
  • Loading branch information
hanazuki authored and bflad committed Dec 20, 2019
1 parent dfed982 commit a6f6977
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion aws/resource_aws_cloudformation_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ func resourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}
}

tInput := cloudformation.GetTemplateInput{
StackName: aws.String(d.Id()),
StackName: aws.String(d.Id()),
TemplateStage: aws.String("Original"),
}
out, err := conn.GetTemplate(&tInput)
if err != nil {
Expand Down
81 changes: 81 additions & 0 deletions aws/resource_aws_cloudformation_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,32 @@ func TestAccAWSCloudFormationStack_withUrl_withParams_noUpdate(t *testing.T) {
})
}

func TestAccAWSCloudFormationStack_withTransform(t *testing.T) {
var stack cloudformation.Stack
rName := fmt.Sprintf("tf-acc-test-with-transform-%s", acctest.RandString(10))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudFormationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudFormationStackConfig_withTransform(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with-transform", &stack),
),
},
{
PlanOnly: true,
Config: testAccAWSCloudFormationStackConfig_withTransform(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with-transform", &stack),
),
},
},
})
}

func testAccCheckCloudFormationStackExists(n string, stack *cloudformation.Stack) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -743,3 +769,58 @@ resource "aws_cloudformation_stack" "test" {
}
`, rName, bucketKey, vpcCidr)
}

func testAccAWSCloudFormationStackConfig_withTransform(rName string) string {
return fmt.Sprintf(`
resource "aws_cloudformation_stack" "with-transform" {
name = "%[1]s"
template_body = <<STACK
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Resources": {
"Api": {
"Type": "AWS::Serverless::Api",
"Properties": {
"StageName": "Prod",
"EndpointConfiguration": "REGIONAL",
"DefinitionBody": {
"swagger": "2.0",
"paths": {
"/": {
"get": {
"consumes": ["application/json"],
"produces": ["application/json"],
"responses": {
"200": {
"description": "200 response"
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200"
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match",
"type": "mock"
}
}
}
}
}
}
}
}
}
STACK
capabilities = ["CAPABILITY_AUTO_EXPAND"]
on_failure = "DELETE"
timeout_in_minutes = 10
}
`, rName)
}

0 comments on commit a6f6977

Please sign in to comment.