2015-06-01 18:33:22 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-21 01:33:14 +01:00
|
|
|
"strings"
|
2015-06-01 18:33:22 +02:00
|
|
|
"testing"
|
|
|
|
|
2015-06-09 21:12:47 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/lambda"
|
2016-01-21 01:33:14 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-06-01 18:33:22 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSLambdaFunction_basic(t *testing.T) {
|
2015-06-01 18:33:22 +02:00
|
|
|
var conf lambda.GetFunctionOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLambdaFunctionDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-02-18 22:45:32 +01:00
|
|
|
Config: testAccAWSLambdaConfigBasic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-01-21 01:33:14 +01:00
|
|
|
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", "example_lambda_name", &conf),
|
|
|
|
testAccCheckAwsLambdaFunctionName(&conf, "example_lambda_name"),
|
|
|
|
testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":example_lambda_name"),
|
2016-02-18 22:45:32 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSLambdaFunction_VPC(t *testing.T) {
|
|
|
|
var conf lambda.GetFunctionOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLambdaFunctionDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSLambdaConfigWithVPC,
|
2015-06-01 18:33:22 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-01-21 01:33:14 +01:00
|
|
|
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", "example_lambda_name", &conf),
|
|
|
|
testAccCheckAwsLambdaFunctionName(&conf, "example_lambda_name"),
|
|
|
|
testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":example_lambda_name"),
|
|
|
|
testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSLambdaFunction_s3(t *testing.T) {
|
|
|
|
var conf lambda.GetFunctionOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLambdaFunctionDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSLambdaConfigS3,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3test", "example_lambda_name_s3", &conf),
|
|
|
|
testAccCheckAwsLambdaFunctionName(&conf, "example_lambda_name_s3"),
|
|
|
|
testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, ":example_lambda_name_s3"),
|
|
|
|
testAccCheckAWSLambdaFunctionVersion(&conf, "$LATEST"),
|
2015-06-01 18:33:22 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckLambdaFunctionDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).lambdaconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_lambda_function" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.GetFunction(&lambda.GetFunctionInput{
|
|
|
|
FunctionName: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Lambda Function still exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:33:14 +01:00
|
|
|
func testAccCheckAwsLambdaFunctionExists(res, funcName string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
|
2015-06-01 18:33:22 +02:00
|
|
|
// Wait for IAM role
|
|
|
|
return func(s *terraform.State) error {
|
2016-01-21 01:33:14 +01:00
|
|
|
rs, ok := s.RootModule().Resources[res]
|
2015-06-01 18:33:22 +02:00
|
|
|
if !ok {
|
2016-01-21 01:33:14 +01:00
|
|
|
return fmt.Errorf("Lambda function not found: %s", res)
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("Lambda function ID not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).lambdaconn
|
|
|
|
|
|
|
|
params := &lambda.GetFunctionInput{
|
2016-01-21 01:33:14 +01:00
|
|
|
FunctionName: aws.String(funcName),
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getFunction, err := conn.GetFunction(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*function = *getFunction
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:33:14 +01:00
|
|
|
func testAccCheckAwsLambdaFunctionName(function *lambda.GetFunctionOutput, expectedName string) resource.TestCheckFunc {
|
2015-06-01 18:33:22 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
c := function.Configuration
|
|
|
|
if *c.FunctionName != expectedName {
|
|
|
|
return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName)
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:33:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSLambdaFunctionVersion(function *lambda.GetFunctionOutput, expectedVersion string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
c := function.Configuration
|
|
|
|
if *c.Version != expectedVersion {
|
|
|
|
return fmt.Errorf("Expected version %s, got %s", expectedVersion, *c.Version)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAwsLambdaFunctionArnHasSuffix(function *lambda.GetFunctionOutput, arnSuffix string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
c := function.Configuration
|
|
|
|
if !strings.HasSuffix(*c.FunctionArn, arnSuffix) {
|
|
|
|
return fmt.Errorf("Expected function ARN %s to have suffix %s", *c.FunctionArn, arnSuffix)
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:45:32 +01:00
|
|
|
const baseAccAWSLambdaConfig = `
|
2016-02-16 06:38:17 +01:00
|
|
|
resource "aws_iam_role_policy" "iam_policy_for_lambda" {
|
|
|
|
name = "iam_policy_for_lambda"
|
|
|
|
role = "${aws_iam_role.iam_for_lambda.id}"
|
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": [
|
|
|
|
"logs:CreateLogGroup",
|
|
|
|
"logs:CreateLogStream",
|
|
|
|
"logs:PutLogEvents"
|
|
|
|
],
|
|
|
|
"Resource": "arn:aws:logs:*:*:*"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": [
|
|
|
|
"ec2:CreateNetworkInterface"
|
|
|
|
],
|
|
|
|
"Resource": [
|
|
|
|
"*"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:33:22 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-16 06:38:17 +01:00
|
|
|
resource "aws_vpc" "vpc_for_lambda" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "subnet_for_lambda" {
|
|
|
|
vpc_id = "${aws_vpc.vpc_for_lambda.id}"
|
|
|
|
cidr_block = "10.0.1.0/24"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "lambda"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "sg_for_lambda" {
|
|
|
|
name = "sg_for_lambda"
|
|
|
|
description = "Allow all inbound traffic for lambda test"
|
|
|
|
vpc_id = "${aws_vpc.vpc_for_lambda.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:45:32 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAWSLambdaConfigBasic = baseAccAWSLambdaConfig + `
|
|
|
|
resource "aws_lambda_function" "lambda_function_test" {
|
|
|
|
filename = "test-fixtures/lambdatest.zip"
|
|
|
|
function_name = "example_lambda_name"
|
|
|
|
role = "${aws_iam_role.iam_for_lambda.arn}"
|
|
|
|
handler = "exports.example"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAWSLambdaConfigWithVPC = baseAccAWSLambdaConfig + `
|
2015-06-01 18:33:22 +02:00
|
|
|
resource "aws_lambda_function" "lambda_function_test" {
|
|
|
|
filename = "test-fixtures/lambdatest.zip"
|
|
|
|
function_name = "example_lambda_name"
|
|
|
|
role = "${aws_iam_role.iam_for_lambda.arn}"
|
|
|
|
handler = "exports.example"
|
2016-02-16 06:38:17 +01:00
|
|
|
|
|
|
|
vpc_config = {
|
|
|
|
subnet_ids = ["${aws_subnet.subnet_for_lambda.id}"]
|
|
|
|
security_group_ids = ["${aws_security_group.sg_for_lambda.id}"]
|
|
|
|
}
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
|
|
|
`
|
2016-01-21 01:33:14 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
`, acctest.RandInt())
|