diff --git a/builtin/providers/aws/resource_aws_lambda_function.go b/builtin/providers/aws/resource_aws_lambda_function.go index 92f6faf9a..3a95134ff 100644 --- a/builtin/providers/aws/resource_aws_lambda_function.go +++ b/builtin/providers/aws/resource_aws_lambda_function.go @@ -323,15 +323,20 @@ func resourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) err d.Set("runtime", function.Runtime) d.Set("timeout", function.Timeout) d.Set("kms_key_arn", function.KMSKeyArn) - if config := flattenLambdaVpcConfigResponse(function.VpcConfig); len(config) > 0 { - log.Printf("[INFO] Setting Lambda %s VPC config %#v from API", d.Id(), config) - err := d.Set("vpc_config", config) - if err != nil { - return fmt.Errorf("Failed setting vpc_config: %s", err) - } + + config := flattenLambdaVpcConfigResponse(function.VpcConfig) + log.Printf("[INFO] Setting Lambda %s VPC config %#v from API", d.Id(), config) + vpcSetErr := d.Set("vpc_config", config) + if vpcSetErr != nil { + return fmt.Errorf("Failed setting vpc_config: %s", vpcSetErr) } + d.Set("source_code_hash", function.CodeSha256) + if err := d.Set("environment", flattenLambdaEnvironment(function.Environment.Variables)); err != nil { + log.Printf("[ERR] Error setting environment for Lambda Function (%s): %s", d.Id(), err) + } + // List is sorted from oldest to latest // so this may get costly over time :'( var lastVersion, lastQualifiedArn string diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 56bab499e..aefb5cdb2 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -950,6 +950,19 @@ func flattenDSVpcSettings( return []map[string]interface{}{settings} } +func flattenLambdaEnvironment(variables map[string]*string) []interface{} { + envs := make(map[string]interface{}) + en := make(map[string]string) + for k, v := range variables { + en[k] = *v + } + if len(en) > 0 { + envs["variables"] = en + } + + return []interface{}{envs} +} + func flattenLambdaVpcConfigResponse(s *lambda.VpcConfigResponse) []map[string]interface{} { settings := make(map[string]interface{}, 0) @@ -957,7 +970,11 @@ func flattenLambdaVpcConfigResponse(s *lambda.VpcConfigResponse) []map[string]in return nil } - if len(s.SubnetIds) == 0 && len(s.SecurityGroupIds) == 0 && s.VpcId == nil { + var emptyVpc bool + if s.VpcId == nil || *s.VpcId == "" { + emptyVpc = true + } + if len(s.SubnetIds) == 0 && len(s.SecurityGroupIds) == 0 && emptyVpc { return nil }