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

data-source/aws_lambda_function: Return unqualified arn attribute by default, properly return error for missing function #7663

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
114 changes: 111 additions & 3 deletions aws/data_source_aws_lambda_function.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package aws

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -16,7 +22,6 @@ func dataSourceAwsLambdaFunction() *schema.Resource {
"qualifier": {
Type: schema.TypeString,
Optional: true,
Default: "$LATEST",
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -152,11 +157,114 @@ func dataSourceAwsLambdaFunction() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(d.Get("function_name").(string))
return resourceAwsLambdaFunctionRead(d, meta)
conn := meta.(*AWSClient).lambdaconn
functionName := d.Get("function_name").(string)

input := &lambda.GetFunctionInput{
FunctionName: aws.String(functionName),
}

if v, ok := d.GetOk("qualifier"); ok {
input.Qualifier = aws.String(v.(string))
}

log.Printf("[DEBUG] Getting Lambda Function: %s", input)
output, err := conn.GetFunction(input)

if err != nil {
return fmt.Errorf("error getting Lambda Function (%s): %s", functionName, err)
}

if output == nil {
return fmt.Errorf("error getting Lambda Function (%s): empty response", functionName)
}

function := output.Configuration

functionARN := aws.StringValue(function.FunctionArn)
qualifierSuffix := fmt.Sprintf(":%s", d.Get("qualifier").(string))
versionSuffix := fmt.Sprintf(":%s", aws.StringValue(function.Version))

qualifiedARN := functionARN
if !strings.HasSuffix(functionARN, qualifierSuffix) && !strings.HasSuffix(functionARN, versionSuffix) {
qualifiedARN = functionARN + versionSuffix
}

unqualifiedARN := strings.TrimSuffix(functionARN, qualifierSuffix)

d.Set("arn", unqualifiedARN)

deadLetterConfig := []interface{}{}
if function.DeadLetterConfig != nil {
deadLetterConfig = []interface{}{
map[string]interface{}{
"target_arn": aws.StringValue(function.DeadLetterConfig.TargetArn),
},
}
}
if err := d.Set("dead_letter_config", deadLetterConfig); err != nil {
return fmt.Errorf("error setting dead_letter_config: %s", err)
}

d.Set("description", function.Description)

if err := d.Set("environment", flattenLambdaEnvironment(function.Environment)); err != nil {
return fmt.Errorf("error setting environment: %s", err)
}

d.Set("handler", function.Handler)
d.Set("invoke_arn", lambdaFunctionInvokeArn(aws.StringValue(function.FunctionArn), meta))
d.Set("kms_key_arn", function.KMSKeyArn)
d.Set("last_modified", function.LastModified)

if err := d.Set("layers", flattenLambdaLayers(function.Layers)); err != nil {
return fmt.Errorf("Error setting layers for Lambda Function (%s): %s", d.Id(), err)
}

d.Set("memory_size", function.MemorySize)
d.Set("qualified_arn", qualifiedARN)

reservedConcurrentExecutions := int64(-1)
if output.Concurrency != nil {
reservedConcurrentExecutions = aws.Int64Value(output.Concurrency.ReservedConcurrentExecutions)
}
d.Set("reserved_concurrent_executions", reservedConcurrentExecutions)

d.Set("role", function.Role)
d.Set("runtime", function.Runtime)
d.Set("source_code_hash", function.CodeSha256)
d.Set("source_code_size", function.CodeSize)

if err := d.Set("tags", tagsToMapGeneric(output.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

tracingConfig := []map[string]interface{}{
{
"mode": lambda.TracingModePassThrough,
},
}
if function.TracingConfig != nil {
tracingConfig[0]["mode"] = aws.StringValue(function.TracingConfig.Mode)
}
if err := d.Set("tracing_config", tracingConfig); err != nil {
return fmt.Errorf("error setting tracing_config: %s", tracingConfig)
}

d.Set("timeout", function.Timeout)
d.Set("version", function.Version)

if err := d.Set("vpc_config", flattenLambdaVpcConfigResponse(function.VpcConfig)); err != nil {
return fmt.Errorf("error setting vpc_config: %s", err)
}

d.SetId(aws.StringValue(function.FunctionName))

return nil
}
Loading