Merge pull request #3794 from robzienert/lambda-s3-support

Adding S3 support for Lambda provider
This commit is contained in:
Clint 2015-11-06 16:16:40 -06:00
commit a6f4c287ae
2 changed files with 52 additions and 17 deletions

View File

@ -13,6 +13,8 @@ import (
"github.com/aws/aws-sdk-go/service/lambda" "github.com/aws/aws-sdk-go/service/lambda"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"errors"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
) )
@ -25,13 +27,28 @@ func resourceAwsLambdaFunction() *schema.Resource {
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"filename": &schema.Schema{ "filename": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, 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"},
}, },
"description": &schema.Schema{ "description": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, // TODO make this editable
}, },
"function_name": &schema.Schema{ "function_name": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
@ -93,22 +110,36 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
log.Printf("[DEBUG] Creating Lambda Function %s with role %s", functionName, iamRole) log.Printf("[DEBUG] Creating Lambda Function %s with role %s", functionName, iamRole)
filename, err := homedir.Expand(d.Get("filename").(string)) var functionCode *lambda.FunctionCode
if err != nil { if v, ok := d.GetOk("filename"); ok {
return err filename, err := homedir.Expand(v.(string))
if err != nil {
return err
}
zipfile, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
d.Set("source_code_hash", sha256.Sum256(zipfile))
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")
if !bucketOk || !keyOk || !versionOk {
return errors.New("s3_bucket, s3_key and s3_object_version 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)),
}
} }
zipfile, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
d.Set("source_code_hash", sha256.Sum256(zipfile))
log.Printf("[DEBUG] ")
params := &lambda.CreateFunctionInput{ params := &lambda.CreateFunctionInput{
Code: &lambda.FunctionCode{ Code: functionCode,
ZipFile: zipfile,
},
Description: aws.String(d.Get("description").(string)), Description: aws.String(d.Get("description").(string)),
FunctionName: aws.String(functionName), FunctionName: aws.String(functionName),
Handler: aws.String(d.Get("handler").(string)), Handler: aws.String(d.Get("handler").(string)),
@ -118,6 +149,7 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
Timeout: aws.Int64(int64(d.Get("timeout").(int))), Timeout: aws.Int64(int64(d.Get("timeout").(int))),
} }
var err error
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
_, err = conn.CreateFunction(params) _, err = conn.CreateFunction(params)
if awsErr, ok := err.(awserr.Error); ok { if awsErr, ok := err.(awserr.Error); ok {

View File

@ -44,7 +44,10 @@ resource "aws_lambda_function" "test_lambda" {
## Argument Reference ## Argument Reference
* `filename` - (Required) A [zip file][2] containing your lambda function source code. * `filename` - (Optional) A [zip file][2] containing your lambda function source code. If defined, The `s3_*` options cannot be used.
* `s3_bucket` - (Optional) The S3 bucket location containing your lambda function source code. Conflicts with `filename`.
* `s3_key` - (Optional) The S3 key containing your lambda function source code. Conflicts with `filename`.
* `s3_object_version` - (Optional) The object version of your lambda function source code. Conflicts with `filename`.
* `function_name` - (Required) A unique name for your Lambda Function. * `function_name` - (Required) A unique name for your Lambda Function.
* `handler` - (Required) The function [entrypoint][3] in your code. * `handler` - (Required) The function [entrypoint][3] in your code.
* `role` - (Required) IAM role attached to the Lambda Function. This governs both who / what can invoke your Lambda Function, as well as what resources our Lambda Function has access to. See [Lambda Permission Model][4] for more details. * `role` - (Required) IAM role attached to the Lambda Function. This governs both who / what can invoke your Lambda Function, as well as what resources our Lambda Function has access to. See [Lambda Permission Model][4] for more details.