-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource: aws_lambda_provisioned_concurrency_config (#11129)
Output from acceptance testing: ``` --- PASS: TestAccAWSLambdaProvisionedConcurrencyConfig_disappears_LambdaProvisionedConcurrencyConfig (240.38s) --- PASS: TestAccAWSLambdaProvisionedConcurrencyConfig_Qualifier_AliasName (246.42s) --- PASS: TestAccAWSLambdaProvisionedConcurrencyConfig_basic (253.46s) --- PASS: TestAccAWSLambdaProvisionedConcurrencyConfig_disappears_LambdaFunction (259.60s) --- PASS: TestAccAWSLambdaProvisionedConcurrencyConfig_ProvisionedConcurrentExecutions (380.90s) ```
- Loading branch information
Showing
6 changed files
with
645 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
aws/resource_aws_lambda_provisioned_concurrency_config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/lambda" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func resourceAwsLambdaProvisionedConcurrencyConfig() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsLambdaProvisionedConcurrencyConfigCreate, | ||
Read: resourceAwsLambdaProvisionedConcurrencyConfigRead, | ||
Update: resourceAwsLambdaProvisionedConcurrencyConfigUpdate, | ||
Delete: resourceAwsLambdaProvisionedConcurrencyConfigDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(15 * time.Minute), | ||
Update: schema.DefaultTimeout(15 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"function_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"provisioned_concurrent_executions": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntAtLeast(1), | ||
}, | ||
"qualifier": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsLambdaProvisionedConcurrencyConfigCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lambdaconn | ||
functionName := d.Get("function_name").(string) | ||
qualifier := d.Get("qualifier").(string) | ||
|
||
input := &lambda.PutProvisionedConcurrencyConfigInput{ | ||
FunctionName: aws.String(functionName), | ||
ProvisionedConcurrentExecutions: aws.Int64(int64(d.Get("provisioned_concurrent_executions").(int))), | ||
Qualifier: aws.String(qualifier), | ||
} | ||
|
||
_, err := conn.PutProvisionedConcurrencyConfig(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error putting Lambda Provisioned Concurrency Config (%s:%s): %s", functionName, qualifier, err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s:%s", functionName, qualifier)) | ||
|
||
if err := waitForLambdaProvisionedConcurrencyConfigStatusReady(conn, functionName, qualifier, d.Timeout(schema.TimeoutCreate)); err != nil { | ||
return fmt.Errorf("error waiting for Lambda Provisioned Concurrency Config (%s) to be ready: %s", d.Id(), err) | ||
} | ||
|
||
return resourceAwsLambdaProvisionedConcurrencyConfigRead(d, meta) | ||
} | ||
|
||
func resourceAwsLambdaProvisionedConcurrencyConfigRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lambdaconn | ||
|
||
functionName, qualifier, err := resourceAwsLambdaProvisionedConcurrencyConfigParseId(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
input := &lambda.GetProvisionedConcurrencyConfigInput{ | ||
FunctionName: aws.String(functionName), | ||
Qualifier: aws.String(qualifier), | ||
} | ||
|
||
output, err := conn.GetProvisionedConcurrencyConfig(input) | ||
|
||
if isAWSErr(err, lambda.ErrCodeProvisionedConcurrencyConfigNotFoundException, "") || isAWSErr(err, lambda.ErrCodeResourceNotFoundException, "") { | ||
log.Printf("[WARN] Lambda Provisioned Concurrency Config (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error getting Lambda Provisioned Concurrency Config (%s): %s", d.Id(), err) | ||
} | ||
|
||
d.Set("function_name", functionName) | ||
d.Set("provisioned_concurrent_executions", aws.Int64Value(output.AllocatedProvisionedConcurrentExecutions)) | ||
d.Set("qualifier", qualifier) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsLambdaProvisionedConcurrencyConfigUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lambdaconn | ||
|
||
functionName, qualifier, err := resourceAwsLambdaProvisionedConcurrencyConfigParseId(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
input := &lambda.PutProvisionedConcurrencyConfigInput{ | ||
FunctionName: aws.String(functionName), | ||
ProvisionedConcurrentExecutions: aws.Int64(int64(d.Get("provisioned_concurrent_executions").(int))), | ||
Qualifier: aws.String(qualifier), | ||
} | ||
|
||
_, err = conn.PutProvisionedConcurrencyConfig(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error putting Lambda Provisioned Concurrency Config (%s:%s): %s", functionName, qualifier, err) | ||
} | ||
|
||
if err := waitForLambdaProvisionedConcurrencyConfigStatusReady(conn, functionName, qualifier, d.Timeout(schema.TimeoutUpdate)); err != nil { | ||
return fmt.Errorf("error waiting for Lambda Provisioned Concurrency Config (%s) to be ready: %s", d.Id(), err) | ||
} | ||
|
||
return resourceAwsLambdaProvisionedConcurrencyConfigRead(d, meta) | ||
} | ||
|
||
func resourceAwsLambdaProvisionedConcurrencyConfigDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).lambdaconn | ||
|
||
functionName, qualifier, err := resourceAwsLambdaProvisionedConcurrencyConfigParseId(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
input := &lambda.DeleteProvisionedConcurrencyConfigInput{ | ||
FunctionName: aws.String(functionName), | ||
Qualifier: aws.String(qualifier), | ||
} | ||
|
||
_, err = conn.DeleteProvisionedConcurrencyConfig(input) | ||
|
||
if isAWSErr(err, lambda.ErrCodeProvisionedConcurrencyConfigNotFoundException, "") || isAWSErr(err, lambda.ErrCodeResourceNotFoundException, "") { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error putting Lambda Provisioned Concurrency Config (%s:%s): %s", functionName, qualifier, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsLambdaProvisionedConcurrencyConfigParseId(id string) (string, string, error) { | ||
parts := strings.SplitN(id, ":", 2) | ||
|
||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
return "", "", fmt.Errorf("unexpected format of ID (%s), expected FUNCTION_NAME:QUALIFIER", id) | ||
} | ||
|
||
return parts[0], parts[1], nil | ||
} | ||
|
||
func refreshLambdaProvisionedConcurrencyConfigStatus(conn *lambda.Lambda, functionName, qualifier string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
input := &lambda.GetProvisionedConcurrencyConfigInput{ | ||
FunctionName: aws.String(functionName), | ||
Qualifier: aws.String(qualifier), | ||
} | ||
|
||
output, err := conn.GetProvisionedConcurrencyConfig(input) | ||
|
||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
status := aws.StringValue(output.Status) | ||
|
||
if status == lambda.ProvisionedConcurrencyStatusEnumFailed { | ||
return output, status, fmt.Errorf("status reason: %s", aws.StringValue(output.StatusReason)) | ||
} | ||
|
||
return output, status, nil | ||
} | ||
} | ||
|
||
func waitForLambdaProvisionedConcurrencyConfigStatusReady(conn *lambda.Lambda, functionName, qualifier string, timeout time.Duration) error { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{lambda.ProvisionedConcurrencyStatusEnumInProgress}, | ||
Target: []string{lambda.ProvisionedConcurrencyStatusEnumReady}, | ||
Refresh: refreshLambdaProvisionedConcurrencyConfigStatus(conn, functionName, qualifier), | ||
Timeout: timeout, | ||
Delay: 5 * time.Second, | ||
} | ||
|
||
_, err := stateConf.WaitForState() | ||
|
||
return err | ||
} |
Oops, something went wrong.