2015-06-01 18:33:22 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2015-12-10 02:42:12 +01:00
|
|
|
"encoding/base64"
|
2015-06-01 18:33:22 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-06-09 21:12:47 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/lambda"
|
2015-06-09 21:27:40 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2015-06-01 18:33:22 +02:00
|
|
|
|
2015-11-06 22:27:47 +01:00
|
|
|
"errors"
|
|
|
|
|
2016-01-05 18:22:57 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-06-01 18:33:22 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsLambdaFunction() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsLambdaFunctionCreate,
|
|
|
|
Read: resourceAwsLambdaFunctionRead,
|
|
|
|
Update: resourceAwsLambdaFunctionUpdate,
|
|
|
|
Delete: resourceAwsLambdaFunctionDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"filename": &schema.Schema{
|
2015-11-06 22:27:47 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"s3_bucket", "s3_key", "s3_object_version"},
|
|
|
|
},
|
|
|
|
"s3_bucket": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"filename"},
|
|
|
|
},
|
|
|
|
"s3_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"filename"},
|
|
|
|
},
|
|
|
|
"s3_object_version": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"filename"},
|
2015-06-01 18:33:22 +02:00
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"function_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"handler": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"memory_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 128,
|
|
|
|
},
|
|
|
|
"role": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"runtime": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: "nodejs",
|
|
|
|
},
|
|
|
|
"timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 3,
|
|
|
|
},
|
2016-02-16 06:38:17 +01:00
|
|
|
"vpc_config": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"subnet_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
"security_group_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-12-10 02:42:12 +01:00
|
|
|
"update_code": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2015-06-01 18:33:22 +02:00
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"last_modified": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"source_code_hash": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
2015-12-10 02:42:12 +01:00
|
|
|
},
|
|
|
|
"remote_code_hash": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
2015-06-01 18:33:22 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resourceAwsLambdaFunction maps to:
|
|
|
|
// CreateFunction in the API / SDK
|
|
|
|
func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).lambdaconn
|
|
|
|
|
|
|
|
functionName := d.Get("function_name").(string)
|
|
|
|
iamRole := d.Get("role").(string)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating Lambda Function %s with role %s", functionName, iamRole)
|
|
|
|
|
2015-11-06 22:27:47 +01:00
|
|
|
var functionCode *lambda.FunctionCode
|
|
|
|
if v, ok := d.GetOk("filename"); ok {
|
2015-11-09 18:39:24 +01:00
|
|
|
zipfile, shaSum, err := loadLocalZipFile(v.(string))
|
2015-11-06 22:27:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-09 18:39:24 +01:00
|
|
|
d.Set("source_code_hash", shaSum)
|
2015-11-06 22:27:47 +01:00
|
|
|
functionCode = &lambda.FunctionCode{
|
|
|
|
ZipFile: zipfile,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s3Bucket, bucketOk := d.GetOk("s3_bucket")
|
|
|
|
s3Key, keyOk := d.GetOk("s3_key")
|
|
|
|
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")
|
2016-01-21 01:33:14 +01:00
|
|
|
if !bucketOk || !keyOk {
|
|
|
|
return errors.New("s3_bucket and s3_key must all be set while using S3 code source")
|
2015-11-06 22:27:47 +01:00
|
|
|
}
|
|
|
|
functionCode = &lambda.FunctionCode{
|
2016-01-21 01:33:14 +01:00
|
|
|
S3Bucket: aws.String(s3Bucket.(string)),
|
|
|
|
S3Key: aws.String(s3Key.(string)),
|
|
|
|
}
|
|
|
|
if versionOk {
|
|
|
|
functionCode.S3ObjectVersion = aws.String(s3ObjectVersion.(string))
|
2015-11-06 22:27:47 +01:00
|
|
|
}
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
params := &lambda.CreateFunctionInput{
|
2015-11-06 22:27:47 +01:00
|
|
|
Code: functionCode,
|
2015-06-01 18:33:22 +02:00
|
|
|
Description: aws.String(d.Get("description").(string)),
|
|
|
|
FunctionName: aws.String(functionName),
|
|
|
|
Handler: aws.String(d.Get("handler").(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
MemorySize: aws.Int64(int64(d.Get("memory_size").(int))),
|
2015-06-01 18:33:22 +02:00
|
|
|
Role: aws.String(iamRole),
|
|
|
|
Runtime: aws.String(d.Get("runtime").(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
Timeout: aws.Int64(int64(d.Get("timeout").(int))),
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
|
|
|
|
2016-02-16 06:38:17 +01:00
|
|
|
if v, ok := d.GetOk("vpc_config"); ok {
|
2016-02-18 22:45:32 +01:00
|
|
|
config, err := validateVPCConfig(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-16 06:38:17 +01:00
|
|
|
|
2016-02-18 22:45:32 +01:00
|
|
|
var subnetIds []*string
|
|
|
|
for _, id := range config["subnet_ids"].(*schema.Set).List() {
|
|
|
|
subnetIds = append(subnetIds, aws.String(id.(string)))
|
|
|
|
}
|
2016-02-16 06:38:17 +01:00
|
|
|
|
2016-02-18 22:45:32 +01:00
|
|
|
var securityGroupIds []*string
|
|
|
|
for _, id := range config["security_group_ids"].(*schema.Set).List() {
|
|
|
|
securityGroupIds = append(securityGroupIds, aws.String(id.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
params.VpcConfig = &lambda.VpcConfig{
|
|
|
|
SubnetIds: subnetIds,
|
|
|
|
SecurityGroupIds: securityGroupIds,
|
2016-02-16 06:38:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 18:22:57 +01:00
|
|
|
// IAM profiles can take ~10 seconds to propagate in AWS:
|
2016-02-18 22:45:32 +01:00
|
|
|
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console
|
2016-01-05 18:22:57 +01:00
|
|
|
// Error creating Lambda function: InvalidParameterValueException: The role defined for the task cannot be assumed by Lambda.
|
2016-03-09 23:53:32 +01:00
|
|
|
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
|
2016-01-05 18:35:21 +01:00
|
|
|
_, err := conn.CreateFunction(params)
|
2016-01-05 18:22:57 +01:00
|
|
|
if err != nil {
|
|
|
|
if awserr, ok := err.(awserr.Error); ok {
|
|
|
|
if awserr.Code() == "InvalidParameterValueException" {
|
2016-02-16 06:38:17 +01:00
|
|
|
log.Printf("[DEBUG] InvalidParameterValueException creating Lambda Function: %s", awserr)
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(awserr)
|
2016-01-05 18:22:57 +01:00
|
|
|
}
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
2016-02-16 06:38:17 +01:00
|
|
|
log.Printf("[DEBUG] Error creating Lambda Function: %s", err)
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
2016-01-05 18:22:57 +01:00
|
|
|
return nil
|
|
|
|
})
|
2015-06-01 18:33:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Lambda function: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(d.Get("function_name").(string))
|
|
|
|
|
|
|
|
return resourceAwsLambdaFunctionRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resourceAwsLambdaFunctionRead maps to:
|
|
|
|
// GetFunction in the API / SDK
|
|
|
|
func resourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).lambdaconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Fetching Lambda Function: %s", d.Id())
|
|
|
|
|
|
|
|
params := &lambda.GetFunctionInput{
|
|
|
|
FunctionName: aws.String(d.Get("function_name").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
getFunctionOutput, err := conn.GetFunction(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFunctionOutput.Code.Location is a pre-signed URL pointing at the zip
|
|
|
|
// file that we uploaded when we created the resource. You can use it to
|
|
|
|
// download the code from AWS. The other part is
|
|
|
|
// getFunctionOutput.Configuration which holds metadata.
|
|
|
|
|
|
|
|
function := getFunctionOutput.Configuration
|
|
|
|
// TODO error checking / handling on the Set() calls.
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("arn", function.FunctionArn)
|
2015-06-01 18:33:22 +02:00
|
|
|
d.Set("description", function.Description)
|
|
|
|
d.Set("handler", function.Handler)
|
|
|
|
d.Set("memory_size", function.MemorySize)
|
|
|
|
d.Set("last_modified", function.LastModified)
|
|
|
|
d.Set("role", function.Role)
|
|
|
|
d.Set("runtime", function.Runtime)
|
|
|
|
d.Set("timeout", function.Timeout)
|
2016-02-18 22:45:32 +01:00
|
|
|
if config := flattenLambdaVpcConfigResponse(function.VpcConfig); len(config) > 0 {
|
|
|
|
d.Set("vpc_config", config)
|
|
|
|
}
|
2015-06-01 18:33:22 +02:00
|
|
|
|
2015-12-10 02:42:12 +01:00
|
|
|
// Compare code hashes, and see if an update is required to code. If there
|
|
|
|
// is, set the "update_code" attribute.
|
|
|
|
|
|
|
|
remoteSum, err := decodeBase64(*function.CodeSha256)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, localSum, err := loadLocalZipFile(d.Get("filename").(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Set("remote_code_hash", remoteSum)
|
|
|
|
d.Set("source_code_hash", localSum)
|
|
|
|
|
|
|
|
if remoteSum != localSum {
|
|
|
|
d.Set("update_code", true)
|
|
|
|
} else {
|
|
|
|
d.Set("update_code", false)
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:33:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resourceAwsLambdaFunction maps to:
|
|
|
|
// DeleteFunction in the API / SDK
|
|
|
|
func resourceAwsLambdaFunctionDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).lambdaconn
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting Lambda Function: %s", d.Id())
|
|
|
|
|
|
|
|
params := &lambda.DeleteFunctionInput{
|
|
|
|
FunctionName: aws.String(d.Get("function_name").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.DeleteFunction(params)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Lambda Function: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resourceAwsLambdaFunctionUpdate maps to:
|
|
|
|
// UpdateFunctionCode in the API / SDK
|
|
|
|
func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-11-09 18:39:24 +01:00
|
|
|
conn := meta.(*AWSClient).lambdaconn
|
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
codeReq := &lambda.UpdateFunctionCodeInput{
|
|
|
|
FunctionName: aws.String(d.Id()),
|
|
|
|
}
|
|
|
|
|
|
|
|
codeUpdate := false
|
|
|
|
if sourceHash, ok := d.GetOk("source_code_hash"); ok {
|
|
|
|
zipfile, shaSum, err := loadLocalZipFile(d.Get("filename").(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sourceHash != shaSum {
|
|
|
|
d.SetPartial("filename")
|
|
|
|
d.SetPartial("source_code_hash")
|
|
|
|
}
|
|
|
|
codeReq.ZipFile = zipfile
|
|
|
|
codeUpdate = true
|
|
|
|
}
|
|
|
|
if d.HasChange("s3_bucket") || d.HasChange("s3_key") || d.HasChange("s3_object_version") {
|
|
|
|
codeReq.S3Bucket = aws.String(d.Get("s3_bucket").(string))
|
|
|
|
codeReq.S3Key = aws.String(d.Get("s3_key").(string))
|
|
|
|
codeReq.S3ObjectVersion = aws.String(d.Get("s3_object_version").(string))
|
|
|
|
codeUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq)
|
|
|
|
if codeUpdate {
|
|
|
|
_, err := conn.UpdateFunctionCode(codeReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error modifying Lambda Function Code %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("filename")
|
|
|
|
d.SetPartial("source_code_hash")
|
|
|
|
d.SetPartial("s3_bucket")
|
|
|
|
d.SetPartial("s3_key")
|
|
|
|
d.SetPartial("s3_object_version")
|
|
|
|
}
|
|
|
|
|
|
|
|
configReq := &lambda.UpdateFunctionConfigurationInput{
|
|
|
|
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("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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq)
|
|
|
|
if configUpdate {
|
|
|
|
_, err := conn.UpdateFunctionConfiguration(configReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
d.SetPartial("description")
|
|
|
|
d.SetPartial("handler")
|
|
|
|
d.SetPartial("memory_size")
|
|
|
|
d.SetPartial("role")
|
|
|
|
d.SetPartial("timeout")
|
|
|
|
}
|
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceAwsLambdaFunctionRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-12-10 02:42:12 +01:00
|
|
|
// loads the local ZIP data and the SHA sum of the data.
|
2015-11-09 18:39:24 +01:00
|
|
|
func loadLocalZipFile(v string) ([]byte, string, error) {
|
|
|
|
filename, err := homedir.Expand(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
zipfile, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
sum := sha256.Sum256(zipfile)
|
2015-12-10 02:42:12 +01:00
|
|
|
return zipfile, fmt.Sprintf("%x", sum), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decodes a base64 string to a string.
|
|
|
|
func decodeBase64(s string) (string, error) {
|
|
|
|
sum, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", sum), nil
|
2015-06-01 18:33:22 +02:00
|
|
|
}
|
2016-02-18 22:45:32 +01:00
|
|
|
|
|
|
|
func validateVPCConfig(v interface{}) (map[string]interface{}, error) {
|
|
|
|
configs := v.([]interface{})
|
|
|
|
if len(configs) > 1 {
|
|
|
|
return nil, errors.New("Only a single vpc_config block is expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
config, ok := configs[0].(map[string]interface{})
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("vpc_config is <nil>")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config["subnet_ids"].(*schema.Set).Len() == 0 {
|
|
|
|
return nil, errors.New("vpc_config.subnet_ids cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config["security_group_ids"].(*schema.Set).Len() == 0 {
|
|
|
|
return nil, errors.New("vpc_config.security_group_ids cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|