Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8506 ability to import an existing lambda alias to terraform state #8513

Merged
merged 2 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions aws/resource_aws_lambda_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func resourceAwsLambdaAlias() *schema.Resource {
Read: resourceAwsLambdaAliasRead,
Update: resourceAwsLambdaAliasUpdate,
Delete: resourceAwsLambdaAliasDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsLambdaAliasImport,
},

Schema: map[string]*schema.Schema{
"description": {
Expand Down Expand Up @@ -117,6 +120,7 @@ func resourceAwsLambdaAliasRead(d *schema.ResourceData, meta interface{}) error
d.Set("function_version", aliasConfiguration.FunctionVersion)
d.Set("name", aliasConfiguration.Name)
d.Set("arn", aliasConfiguration.AliasArn)
d.SetId(*aliasConfiguration.AliasArn)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically d.SetId() should only be called during Create and Importer State functions, however this is understandably a little more nuanced here since if we followed that rule here we would need the entire alias ARN passed into terraform import or to make an additional API call in the import function which is less than ideal. I'm okay with merging this as-is for now to get this functionality in. 👍


invokeArn := lambdaFunctionInvokeArn(*aliasConfiguration.AliasArn, meta)
d.Set("invoke_arn", invokeArn)
Expand Down Expand Up @@ -186,3 +190,25 @@ func expandLambdaAliasRoutingConfiguration(l []interface{}) *lambda.AliasRouting

return aliasRoutingConfiguration
}

func resourceAwsLambdaAliasImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected FUNCTION_NAME/ALIAS", d.Id())
}

functionName := idParts[0]
alias := idParts[1]
log.Printf("[DEBUG] Importing Lambda Alias %s for function name %s", alias, functionName)

conn := meta.(*AWSClient).lambdaconn

getFunctionOutput, err := conn.GetFunction(&lambda.GetFunctionInput{FunctionName: &functionName})
if err != nil {
return nil, err
}

d.Set("function_name", getFunctionOutput.Configuration.FunctionArn)
d.Set("name", alias)
return []*schema.ResourceData{d}, nil
}
6 changes: 6 additions & 0 deletions aws/resource_aws_lambda_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func TestAccAWSLambdaAlias_basic(t *testing.T) {
resource.TestMatchResourceAttr(resourceName, "invoke_arn", regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:apigateway:[^:]+:lambda:path/2015-03-31/functions/arn:[^:]+:lambda:[^:]+:[^:]+:function:%s:%s/invocations$", funcName, aliasName))),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s", funcName, aliasName),
ImportStateVerify: true,
},
},
})
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/lambda_alias.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ For **routing_config** the following attributes are supported:
[1]: http://docs.aws.amazon.com/lambda/latest/dg/welcome.html
[2]: http://docs.aws.amazon.com/lambda/latest/dg/API_CreateAlias.html
[3]: https://docs.aws.amazon.com/lambda/latest/dg/API_AliasRoutingConfiguration.html

## Import

Lambda Function Aliases can be imported using the `function_name/alias`, e.g.

```
$ terraform import aws_lambda_function_alias.test_lambda_alias my_test_lambda_function/my_alias
```