Skip to content

Commit

Permalink
Merge pull request #15121 from grahamhar/master
Browse files Browse the repository at this point in the history
Update plan output to reflect publish(version, qualified_arn) when config changes
  • Loading branch information
gdavison authored Oct 29, 2020
2 parents 8f9f140 + af107a7 commit f3f6841
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
37 changes: 15 additions & 22 deletions aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,24 @@ func resourceAwsLambdaFunction() *schema.Resource {
}

func updateComputedAttributesOnPublish(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
if needsFunctionCodeUpdate(d) {
configChanged := hasConfigChanges(d)
functionCodeUpdated := needsFunctionCodeUpdate(d)
if functionCodeUpdated {
d.SetNewComputed("last_modified")
publish := d.Get("publish").(bool)
if publish {
d.SetNewComputed("version")
d.SetNewComputed("qualified_arn")
}
}

publish := d.Get("publish").(bool)
if publish && (configChanged || functionCodeUpdated) {
d.SetNewComputed("version")
d.SetNewComputed("qualified_arn")
}
return nil
}

func hasConfigChanges(d resourceDiffer) bool {
return d.HasChange("description") || d.HasChange("handler") || d.HasChange("file_system_config") || d.HasChange("memory_size") || d.HasChange("role") || d.HasChange("timeout") || d.HasChange("kms_key_arn") || d.HasChange("layers") || d.HasChange("dead_letter_config") || d.HasChange("tracing_config") || d.HasChange("vpc_config") || d.HasChange("runtime") || d.HasChange("environment")
}

// resourceAwsLambdaFunction maps to:
// CreateFunction in the API / SDK
func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -698,42 +705,33 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
FunctionName: aws.String(d.Id()),
}

configUpdate := false
if d.HasChange("description") {
configReq.Description = aws.String(d.Get("description").(string))
configUpdate = true
}
if d.HasChange("handler") {
configReq.Handler = aws.String(d.Get("handler").(string))
configUpdate = true
}
if d.HasChange("file_system_config") {
configReq.FileSystemConfigs = make([]*lambda.FileSystemConfig, 0)
if v, ok := d.GetOk("file_system_config"); ok && len(v.([]interface{})) > 0 {
configReq.FileSystemConfigs = expandLambdaFileSystemConfigs(v.([]interface{}))
}
configUpdate = true
}
if d.HasChange("memory_size") {
configReq.MemorySize = aws.Int64(int64(d.Get("memory_size").(int)))
configUpdate = true
}
if d.HasChange("role") {
configReq.Role = aws.String(d.Get("role").(string))
configUpdate = true
}
if d.HasChange("timeout") {
configReq.Timeout = aws.Int64(int64(d.Get("timeout").(int)))
configUpdate = true
}
if d.HasChange("kms_key_arn") {
configReq.KMSKeyArn = aws.String(d.Get("kms_key_arn").(string))
configUpdate = true
}
if d.HasChange("layers") {
layers := d.Get("layers").([]interface{})
configReq.Layers = expandStringList(layers)
configUpdate = true
}
if d.HasChange("dead_letter_config") {
dlcMaps := d.Get("dead_letter_config").([]interface{})
Expand All @@ -744,7 +742,6 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
dlcMap := dlcMaps[0].(map[string]interface{})
configReq.DeadLetterConfig.TargetArn = aws.String(dlcMap["target_arn"].(string))
}
configUpdate = true
}
if d.HasChange("tracing_config") {
tracingConfig := d.Get("tracing_config").([]interface{})
Expand All @@ -753,7 +750,6 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
configReq.TracingConfig = &lambda.TracingConfig{
Mode: aws.String(config["mode"].(string)),
}
configUpdate = true
}
}
if d.HasChange("vpc_config") {
Expand All @@ -766,12 +762,10 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
configReq.VpcConfig.SecurityGroupIds = expandStringSet(vpcConfig["security_group_ids"].(*schema.Set))
configReq.VpcConfig.SubnetIds = expandStringSet(vpcConfig["subnet_ids"].(*schema.Set))
}
configUpdate = true
}

if d.HasChange("runtime") {
configReq.Runtime = aws.String(d.Get("runtime").(string))
configUpdate = true
}
if d.HasChange("environment") {
if v, ok := d.GetOk("environment"); ok {
Expand All @@ -787,16 +781,15 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
configReq.Environment = &lambda.Environment{
Variables: aws.StringMap(variables),
}
configUpdate = true
}
} else {
configReq.Environment = &lambda.Environment{
Variables: aws.StringMap(map[string]string{}),
}
configUpdate = true

}
}

configUpdate := hasConfigChanges(d)
if configUpdate {
log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq)

Expand Down
2 changes: 0 additions & 2 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,6 @@ resource "aws_efs_access_point" "access_point_1" {
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
publish = true
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
runtime = "nodejs12.x"
Expand Down Expand Up @@ -2009,7 +2008,6 @@ resource "aws_efs_access_point" "access_point_2" {
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
publish = true
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.example"
runtime = "nodejs12.x"
Expand Down

0 comments on commit f3f6841

Please sign in to comment.