-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
resource_aws_lambda_function.go
855 lines (753 loc) · 24.8 KB
/
resource_aws_lambda_function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
package aws
import (
"fmt"
"io/ioutil"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/mitchellh/go-homedir"
"errors"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
const awsMutexLambdaKey = `aws_lambda_function`
func resourceAwsLambdaFunction() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLambdaFunctionCreate,
Read: resourceAwsLambdaFunctionRead,
Update: resourceAwsLambdaFunctionUpdate,
Delete: resourceAwsLambdaFunctionDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("function_name", d.Id())
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"filename": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"s3_bucket", "s3_key", "s3_object_version"},
},
"s3_bucket": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"filename"},
},
"s3_key": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"filename"},
},
"s3_object_version": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"filename"},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"dead_letter_config": {
Type: schema.TypeList,
Optional: true,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
},
},
},
"function_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"handler": {
Type: schema.TypeString,
Required: true,
},
"memory_size": {
Type: schema.TypeInt,
Optional: true,
Default: 128,
},
"reserved_concurrent_executions": {
Type: schema.TypeInt,
Optional: true,
},
"role": {
Type: schema.TypeString,
Required: true,
},
"runtime": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
// lambda.RuntimeNodejs has reached end of life since October 2016 so not included here
lambda.RuntimeNodejs43,
lambda.RuntimeNodejs610,
lambda.RuntimeJava8,
lambda.RuntimePython27,
lambda.RuntimePython36,
lambda.RuntimeDotnetcore10,
lambda.RuntimeDotnetcore20,
lambda.RuntimeNodejs43Edge,
lambda.RuntimeGo1X,
}, false),
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 3,
},
"publish": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
"vpc_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_ids": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"security_group_ids": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"qualified_arn": {
Type: schema.TypeString,
Computed: true,
},
"invoke_arn": {
Type: schema.TypeString,
Computed: true,
},
"last_modified": {
Type: schema.TypeString,
Computed: true,
},
"source_code_hash": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"environment": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"variables": {
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
},
},
},
},
"tracing_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mode": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"Active", "PassThrough"}, true),
},
},
},
},
"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"tags": tagsSchema(),
},
CustomizeDiff: updateComputedAttributesOnPublish,
}
}
func updateComputedAttributesOnPublish(d *schema.ResourceDiff, meta interface{}) error {
if needsFunctionCodeUpdate(d) {
d.SetNewComputed("last_modified")
publish := d.Get("publish").(bool)
if publish {
d.SetNewComputed("version")
d.SetNewComputed("qualified_arn")
}
}
return nil
}
// 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)
reservedConcurrentExecutions := d.Get("reserved_concurrent_executions").(int)
iamRole := d.Get("role").(string)
log.Printf("[DEBUG] Creating Lambda Function %s with role %s", functionName, iamRole)
filename, hasFilename := d.GetOk("filename")
s3Bucket, bucketOk := d.GetOk("s3_bucket")
s3Key, keyOk := d.GetOk("s3_key")
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")
if !hasFilename && !bucketOk && !keyOk && !versionOk {
return errors.New("filename or s3_* attributes must be set")
}
var functionCode *lambda.FunctionCode
if hasFilename {
// Grab an exclusive lock so that we're only reading one function into
// memory at a time.
// See https://github.com/hashicorp/terraform/issues/9364
awsMutexKV.Lock(awsMutexLambdaKey)
defer awsMutexKV.Unlock(awsMutexLambdaKey)
file, err := loadFileContent(filename.(string))
if err != nil {
return fmt.Errorf("Unable to load %q: %s", filename.(string), err)
}
functionCode = &lambda.FunctionCode{
ZipFile: file,
}
} else {
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)),
}
if versionOk {
functionCode.S3ObjectVersion = aws.String(s3ObjectVersion.(string))
}
}
params := &lambda.CreateFunctionInput{
Code: functionCode,
Description: aws.String(d.Get("description").(string)),
FunctionName: aws.String(functionName),
Handler: aws.String(d.Get("handler").(string)),
MemorySize: aws.Int64(int64(d.Get("memory_size").(int))),
Role: aws.String(iamRole),
Runtime: aws.String(d.Get("runtime").(string)),
Timeout: aws.Int64(int64(d.Get("timeout").(int))),
Publish: aws.Bool(d.Get("publish").(bool)),
}
if v, ok := d.GetOk("dead_letter_config"); ok {
dlcMaps := v.([]interface{})
if len(dlcMaps) == 1 { // Schema guarantees either 0 or 1
// Prevent panic on nil dead_letter_config. See GH-14961
if dlcMaps[0] == nil {
return fmt.Errorf("Nil dead_letter_config supplied for function: %s", functionName)
}
dlcMap := dlcMaps[0].(map[string]interface{})
params.DeadLetterConfig = &lambda.DeadLetterConfig{
TargetArn: aws.String(dlcMap["target_arn"].(string)),
}
}
}
if v, ok := d.GetOk("vpc_config"); ok {
configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})
if !ok {
return errors.New("vpc_config is <nil>")
}
if config != nil {
var subnetIds []*string
for _, id := range config["subnet_ids"].(*schema.Set).List() {
subnetIds = append(subnetIds, aws.String(id.(string)))
}
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,
}
}
}
if v, ok := d.GetOk("tracing_config"); ok {
tracingConfig := v.([]interface{})
tracing := tracingConfig[0].(map[string]interface{})
params.TracingConfig = &lambda.TracingConfig{
Mode: aws.String(tracing["mode"].(string)),
}
}
if v, ok := d.GetOk("environment"); ok {
environments := v.([]interface{})
environment, ok := environments[0].(map[string]interface{})
if !ok {
return errors.New("At least one field is expected inside environment")
}
if environmentVariables, ok := environment["variables"]; ok {
variables := readEnvironmentVariables(environmentVariables.(map[string]interface{}))
params.Environment = &lambda.Environment{
Variables: aws.StringMap(variables),
}
}
}
if v, ok := d.GetOk("kms_key_arn"); ok {
params.KMSKeyArn = aws.String(v.(string))
}
if v, exists := d.GetOk("tags"); exists {
params.Tags = tagsFromMapGeneric(v.(map[string]interface{}))
}
// IAM changes can take 1 minute to propagate in AWS
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.CreateFunction(params)
if err != nil {
log.Printf("[DEBUG] Error creating Lambda Function: %s", err)
if isAWSErr(err, "InvalidParameterValueException", "The role defined for the function cannot be assumed by Lambda") {
log.Printf("[DEBUG] Received %s, retrying CreateFunction", err)
return resource.RetryableError(err)
}
if isAWSErr(err, "InvalidParameterValueException", "The provided execution role does not have permissions") {
log.Printf("[DEBUG] Received %s, retrying CreateFunction", err)
return resource.RetryableError(err)
}
if isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2") {
log.Printf("[DEBUG] Received %s, retrying CreateFunction", err)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
if !isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2") {
return fmt.Errorf("Error creating Lambda function: %s", err)
}
// Allow 9 more minutes for EC2 throttling
err := resource.Retry(9*time.Minute, func() *resource.RetryError {
_, err := conn.CreateFunction(params)
if err != nil {
log.Printf("[DEBUG] Error creating Lambda Function: %s", err)
if isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2") {
log.Printf("[DEBUG] Received %s, retrying CreateFunction", err)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error creating Lambda function: %s", err)
}
}
d.SetId(d.Get("function_name").(string))
if reservedConcurrentExecutions > 0 {
log.Printf("[DEBUG] Setting Concurrency to %d for the Lambda Function %s", reservedConcurrentExecutions, functionName)
concurrencyParams := &lambda.PutFunctionConcurrencyInput{
FunctionName: aws.String(functionName),
ReservedConcurrentExecutions: aws.Int64(int64(reservedConcurrentExecutions)),
}
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.PutFunctionConcurrency(concurrencyParams)
if err != nil {
if isAWSErr(err, lambda.ErrCodeResourceNotFoundException, "") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error setting concurrency for Lambda %s: %s", functionName, err)
}
}
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 {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" && !d.IsNewResource() {
d.SetId("")
return 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.
d.Set("arn", function.FunctionArn)
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)
d.Set("kms_key_arn", function.KMSKeyArn)
d.Set("tags", tagsToMapGeneric(getFunctionOutput.Tags))
config := flattenLambdaVpcConfigResponse(function.VpcConfig)
log.Printf("[INFO] Setting Lambda %s VPC config %#v from API", d.Id(), config)
vpcSetErr := d.Set("vpc_config", config)
if vpcSetErr != nil {
return fmt.Errorf("Failed setting vpc_config: %s", vpcSetErr)
}
d.Set("source_code_hash", function.CodeSha256)
if err := d.Set("environment", flattenLambdaEnvironment(function.Environment)); err != nil {
log.Printf("[ERR] Error setting environment for Lambda Function (%s): %s", d.Id(), err)
}
if function.DeadLetterConfig != nil && function.DeadLetterConfig.TargetArn != nil {
d.Set("dead_letter_config", []interface{}{
map[string]interface{}{
"target_arn": *function.DeadLetterConfig.TargetArn,
},
})
} else {
d.Set("dead_letter_config", []interface{}{})
}
if function.TracingConfig != nil {
d.Set("tracing_config", []interface{}{
map[string]interface{}{
"mode": *function.TracingConfig.Mode,
},
})
}
// List is sorted from oldest to latest
// so this may get costly over time :'(
var lastVersion, lastQualifiedArn string
err = listVersionsByFunctionPages(conn, &lambda.ListVersionsByFunctionInput{
FunctionName: function.FunctionName,
MaxItems: aws.Int64(10000),
}, func(p *lambda.ListVersionsByFunctionOutput, lastPage bool) bool {
if lastPage {
last := p.Versions[len(p.Versions)-1]
lastVersion = *last.Version
lastQualifiedArn = *last.FunctionArn
return false
}
return true
})
if err != nil {
return err
}
d.Set("version", lastVersion)
d.Set("qualified_arn", lastQualifiedArn)
d.Set("invoke_arn", buildLambdaInvokeArn(*function.FunctionArn, meta.(*AWSClient).region))
if getFunctionOutput.Concurrency != nil {
d.Set("reserved_concurrent_executions", getFunctionOutput.Concurrency.ReservedConcurrentExecutions)
} else {
d.Set("reserved_concurrent_executions", nil)
}
return nil
}
func listVersionsByFunctionPages(c *lambda.Lambda, input *lambda.ListVersionsByFunctionInput,
fn func(p *lambda.ListVersionsByFunctionOutput, lastPage bool) bool) error {
for {
page, err := c.ListVersionsByFunction(input)
if err != nil {
return err
}
lastPage := page.NextMarker == nil
shouldContinue := fn(page, lastPage)
if !shouldContinue || lastPage {
break
}
input.Marker = page.NextMarker
}
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
}
type resourceDiffer interface {
HasChange(string) bool
}
func needsFunctionCodeUpdate(d resourceDiffer) bool {
return d.HasChange("filename") || d.HasChange("source_code_hash") || d.HasChange("s3_bucket") || d.HasChange("s3_key") || d.HasChange("s3_object_version")
}
// resourceAwsLambdaFunctionUpdate maps to:
// UpdateFunctionCode in the API / SDK
func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
d.Partial(true)
arn := d.Get("arn").(string)
if tagErr := setTagsLambda(conn, d, arn); tagErr != nil {
return tagErr
}
d.SetPartial("tags")
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
}
if d.HasChange("kms_key_arn") {
configReq.KMSKeyArn = aws.String(d.Get("kms_key_arn").(string))
configUpdate = true
}
if d.HasChange("dead_letter_config") {
dlcMaps := d.Get("dead_letter_config").([]interface{})
if len(dlcMaps) == 1 { // Schema guarantees either 0 or 1
dlcMap := dlcMaps[0].(map[string]interface{})
configReq.DeadLetterConfig = &lambda.DeadLetterConfig{
TargetArn: aws.String(dlcMap["target_arn"].(string)),
}
configUpdate = true
}
}
if d.HasChange("tracing_config") {
tracingConfig := d.Get("tracing_config").([]interface{})
if len(tracingConfig) == 1 { // Schema guarantees either 0 or 1
config := tracingConfig[0].(map[string]interface{})
configReq.TracingConfig = &lambda.TracingConfig{
Mode: aws.String(config["mode"].(string)),
}
configUpdate = true
}
}
if d.HasChange("vpc_config") {
vpcConfigRaw := d.Get("vpc_config").([]interface{})
vpcConfig, ok := vpcConfigRaw[0].(map[string]interface{})
if !ok {
return errors.New("vpc_config is <nil>")
}
if vpcConfig != nil {
var subnetIds []*string
for _, id := range vpcConfig["subnet_ids"].(*schema.Set).List() {
subnetIds = append(subnetIds, aws.String(id.(string)))
}
var securityGroupIds []*string
for _, id := range vpcConfig["security_group_ids"].(*schema.Set).List() {
securityGroupIds = append(securityGroupIds, aws.String(id.(string)))
}
configReq.VpcConfig = &lambda.VpcConfig{
SubnetIds: subnetIds,
SecurityGroupIds: securityGroupIds,
}
configUpdate = true
}
}
if d.HasChange("runtime") {
configReq.Runtime = aws.String(d.Get("runtime").(string))
configUpdate = true
}
if d.HasChange("environment") {
if v, ok := d.GetOk("environment"); ok {
environments := v.([]interface{})
environment, ok := environments[0].(map[string]interface{})
if !ok {
return errors.New("At least one field is expected inside environment")
}
if environmentVariables, ok := environment["variables"]; ok {
variables := readEnvironmentVariables(environmentVariables.(map[string]interface{}))
configReq.Environment = &lambda.Environment{
Variables: aws.StringMap(variables),
}
configUpdate = true
}
} else {
configReq.Environment = &lambda.Environment{
Variables: aws.StringMap(map[string]string{}),
}
configUpdate = true
}
}
if configUpdate {
log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq)
// IAM changes can take 1 minute to propagate in AWS
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.UpdateFunctionConfiguration(configReq)
if err != nil {
log.Printf("[DEBUG] Received error modifying Lambda Function Configuration %s: %s", d.Id(), err)
if isAWSErr(err, "InvalidParameterValueException", "The role defined for the function cannot be assumed by Lambda") {
log.Printf("[DEBUG] Received %s, retrying UpdateFunctionConfiguration", err)
return resource.RetryableError(err)
}
if isAWSErr(err, "InvalidParameterValueException", "The provided execution role does not have permissions") {
log.Printf("[DEBUG] Received %s, retrying UpdateFunctionConfiguration", err)
return resource.RetryableError(err)
}
if isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2, please make sure you have enough API rate limit.") {
log.Printf("[DEBUG] Received %s, retrying UpdateFunctionConfiguration", err)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
if !isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2, please make sure you have enough API rate limit.") {
return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err)
}
// Allow 9 more minutes for EC2 throttling
err := resource.Retry(9*time.Minute, func() *resource.RetryError {
_, err := conn.UpdateFunctionConfiguration(configReq)
if err != nil {
log.Printf("[DEBUG] Received error modifying Lambda Function Configuration %s: %s", d.Id(), err)
if isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2, please make sure you have enough API rate limit.") {
log.Printf("[DEBUG] Received %s, retrying UpdateFunctionConfiguration", err)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
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")
}
if needsFunctionCodeUpdate(d) {
codeReq := &lambda.UpdateFunctionCodeInput{
FunctionName: aws.String(d.Id()),
Publish: aws.Bool(d.Get("publish").(bool)),
}
if v, ok := d.GetOk("filename"); ok {
// Grab an exclusive lock so that we're only reading one function into
// memory at a time.
// See https://github.com/hashicorp/terraform/issues/9364
awsMutexKV.Lock(awsMutexLambdaKey)
defer awsMutexKV.Unlock(awsMutexLambdaKey)
file, err := loadFileContent(v.(string))
if err != nil {
return fmt.Errorf("Unable to load %q: %s", v.(string), err)
}
codeReq.ZipFile = file
} else {
s3Bucket, _ := d.GetOk("s3_bucket")
s3Key, _ := d.GetOk("s3_key")
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")
codeReq.S3Bucket = aws.String(s3Bucket.(string))
codeReq.S3Key = aws.String(s3Key.(string))
if versionOk {
codeReq.S3ObjectVersion = aws.String(s3ObjectVersion.(string))
}
}
log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq)
_, 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")
}
if d.HasChange("reserved_concurrent_executions") {
nc := d.Get("reserved_concurrent_executions")
if nc.(int) > 0 {
log.Printf("[DEBUG] Updating Concurrency to %d for the Lambda Function %s", nc.(int), d.Id())
concurrencyParams := &lambda.PutFunctionConcurrencyInput{
FunctionName: aws.String(d.Id()),
ReservedConcurrentExecutions: aws.Int64(int64(d.Get("reserved_concurrent_executions").(int))),
}
_, err := conn.PutFunctionConcurrency(concurrencyParams)
if err != nil {
return fmt.Errorf("Error setting concurrency for Lambda %s: %s", d.Id(), err)
}
} else {
log.Printf("[DEBUG] Removing Concurrency for the Lambda Function %s", d.Id())
deleteConcurrencyParams := &lambda.DeleteFunctionConcurrencyInput{
FunctionName: aws.String(d.Id()),
}
_, err := conn.DeleteFunctionConcurrency(deleteConcurrencyParams)
if err != nil {
return fmt.Errorf("Error setting concurrency for Lambda %s: %s", d.Id(), err)
}
}
}
d.Partial(false)
return resourceAwsLambdaFunctionRead(d, meta)
}
// loadFileContent returns contents of a file in a given path
func loadFileContent(v string) ([]byte, error) {
filename, err := homedir.Expand(v)
if err != nil {
return nil, err
}
fileContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return fileContent, nil
}
func readEnvironmentVariables(ev map[string]interface{}) map[string]string {
variables := make(map[string]string)
for k, v := range ev {
variables[k] = v.(string)
}
return variables
}