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

Lambda S3 object version defaults to $LATEST if unspecified #4770

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
14 changes: 9 additions & 5 deletions builtin/providers/aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
s3Bucket, bucketOk := d.GetOk("s3_bucket")
s3Key, keyOk := d.GetOk("s3_key")
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")
if !bucketOk || !keyOk || !versionOk {
return errors.New("s3_bucket, s3_key and s3_object_version must all be set while using S3 code source")
if !bucketOk || !keyOk {
return errors.New("s3_bucket and s3_key must all be set while using S3 code source")
}
functionCode = &lambda.FunctionCode{
S3Bucket: aws.String(s3Bucket.(string)),
S3Key: aws.String(s3Key.(string)),
S3ObjectVersion: aws.String(s3ObjectVersion.(string)),
S3Bucket: aws.String(s3Bucket.(string)),
S3Key: aws.String(s3Key.(string)),
}
if versionOk {
functionCode.S3ObjectVersion = aws.String(s3ObjectVersion.(string))
}
}

Expand All @@ -155,6 +157,8 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
err := resource.Retry(1*time.Minute, func() error {
_, err := conn.CreateFunction(params)
if err != nil {
log.Printf("[DEBUG] Error creating Lambda Function: %s", err)

if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "InvalidParameterValueException" {
// Retryable
Expand Down
72 changes: 62 additions & 10 deletions builtin/providers/aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package aws

import (
"fmt"
"math/rand"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
Expand All @@ -21,8 +24,15 @@ func TestAccAWSLambdaFunction_basic(t *testing.T) {
resource.TestStep{
Config: testAccAWSLambdaConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", &conf),
testAccCheckAWSLambdaAttributes(&conf),
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", "example_lambda_name", &conf),
testAccCheckAWSLambdaAttributes("example_lambda_name", &conf),
),
},
resource.TestStep{
Config: testAccAWSLambdaConfigS3,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3test", "example_lambda_name_s3", &conf),
testAccCheckAWSLambdaAttributes("example_lambda_name_s3", &conf),
),
},
},
Expand Down Expand Up @@ -51,12 +61,12 @@ func testAccCheckLambdaFunctionDestroy(s *terraform.State) error {

}

func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
func testAccCheckAwsLambdaFunctionExists(res, fname string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
// Wait for IAM role
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
rs, ok := s.RootModule().Resources[res]
if !ok {
return fmt.Errorf("Lambda function not found: %s", n)
return fmt.Errorf("Lambda function not found: %s", res)
}

if rs.Primary.ID == "" {
Expand All @@ -66,7 +76,7 @@ func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionO
conn := testAccProvider.Meta().(*AWSClient).lambdaconn

params := &lambda.GetFunctionInput{
FunctionName: aws.String("example_lambda_name"),
FunctionName: aws.String(fname),
}

getFunction, err := conn.GetFunction(params)
Expand All @@ -80,18 +90,21 @@ func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionO
}
}

func testAccCheckAWSLambdaAttributes(function *lambda.GetFunctionOutput) resource.TestCheckFunc {
func testAccCheckAWSLambdaAttributes(fname string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
c := function.Configuration
const expectedName = "example_lambda_name"
if *c.FunctionName != expectedName {
return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName)
if *c.FunctionName != fname {
return fmt.Errorf("Expected function name %s, got %s", fname, *c.FunctionName)
}

if *c.FunctionArn == "" {
return fmt.Errorf("Could not read Lambda Function's ARN")
}

if strings.HasSuffix(fname, "s3") && *c.Version != "$LATEST" {
return fmt.Errorf("Expected version $LATEST, got %s", *c.Version)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to separate this function out into more generic ones instead of hardcoding values, e.g.

  • testAccCheckAWSLambdaFunctionName(expectedName string, function *lambda.GetFunctionOutput)
  • testAccCheckAWSLambdaFunctionVersion(expectedVersion string, function *lambda.GetFunctionOutput)

return nil
}
}
Expand Down Expand Up @@ -123,3 +136,42 @@ resource "aws_lambda_function" "lambda_function_test" {
handler = "exports.example"
}
`

var testAccAWSLambdaConfigS3 = fmt.Sprintf(`
resource "aws_s3_bucket" "lambda_bucket" {
bucket = "tf-test-bucket-%d"
}

resource "aws_s3_bucket_object" "lambda_code" {
bucket = "${aws_s3_bucket.lambda_bucket.id}"
key = "lambdatest.zip"
source = "test-fixtures/lambdatest.zip"
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_lambda_function" "lambda_function_s3test" {
s3_bucket = "${aws_s3_bucket.lambda_bucket.id}"
s3_key = "${aws_s3_bucket_object.lambda_code.id}"
function_name = "example_lambda_name_s3"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
}
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())