github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/website/source/docs/providers/aws/r/lambda_permission.html.markdown (about) 1 --- 2 layout: "aws" 3 page_title: "AWS: aws_lambda_permission" 4 sidebar_current: "docs-aws-resource-lambda-permission" 5 description: |- 6 Creates a Lambda function permission. 7 --- 8 9 # aws\_lambda\_permission 10 11 Creates a Lambda permission to allow external sources invoking the Lambda function 12 (e.g. CloudWatch Event Rule, SNS or S3). 13 14 ## Example Usage 15 16 ``` 17 resource "aws_lambda_permission" "allow_cloudwatch" { 18 statement_id = "AllowExecutionFromCloudWatch" 19 action = "lambda:InvokeFunction" 20 function_name = "${aws_lambda_function.test_lambda.function_name}" 21 principal = "events.amazonaws.com" 22 source_account = "111122223333" 23 source_arn = "arn:aws:events:eu-west-1:111122223333:rule/RunDaily" 24 qualifier = "${aws_lambda_alias.test_alias.name}" 25 } 26 27 resource "aws_lambda_alias" "test_alias" { 28 name = "testalias" 29 description = "a sample description" 30 function_name = "${aws_lambda_function.test_lambda.arn}" 31 function_version = "$LATEST" 32 } 33 34 resource "aws_lambda_function" "test_lambda" { 35 filename = "lambdatest.zip" 36 function_name = "lambda_function_name" 37 role = "${aws_iam_role.iam_for_lambda.arn}" 38 handler = "exports.handler" 39 } 40 41 resource "aws_iam_role" "iam_for_lambda" { 42 name = "iam_for_lambda" 43 44 assume_role_policy = <<EOF 45 { 46 "Version": "2012-10-17", 47 "Statement": [ 48 { 49 "Action": "sts:AssumeRole", 50 "Principal": { 51 "Service": "lambda.amazonaws.com" 52 }, 53 "Effect": "Allow", 54 "Sid": "" 55 } 56 ] 57 } 58 EOF 59 } 60 ``` 61 62 ## Usage with SNS 63 64 ``` 65 resource "aws_lambda_permission" "with_sns" { 66 statement_id = "AllowExecutionFromSNS" 67 action = "lambda:InvokeFunction" 68 function_name = "${aws_lambda_function.my-func.arn}" 69 principal = "sns.amazonaws.com" 70 source_arn = "${aws_sns_topic.default.arn}" 71 } 72 73 resource "aws_sns_topic" "default" { 74 name = "call-lambda-maybe" 75 } 76 77 resource "aws_sns_topic_subscription" "lambda" { 78 topic_arn = "${aws_sns_topic.default.arn}" 79 protocol = "lambda" 80 endpoint = "${aws_lambda_function.func.arn}" 81 } 82 83 resource "aws_lambda_function" "func" { 84 filename = "lambdatest.zip" 85 function_name = "lambda_called_from_sns" 86 role = "${aws_iam_role.default.arn}" 87 handler = "exports.handler" 88 } 89 90 resource "aws_iam_role" "default" { 91 name = "iam_for_lambda_with_sns" 92 93 assume_role_policy = <<EOF 94 { 95 "Version": "2012-10-17", 96 "Statement": [ 97 { 98 "Action": "sts:AssumeRole", 99 "Principal": { 100 "Service": "lambda.amazonaws.com" 101 }, 102 "Effect": "Allow", 103 "Sid": "" 104 } 105 ] 106 } 107 EOF 108 } 109 ``` 110 111 ## Argument Reference 112 113 * `action` - (Required) The AWS Lambda action you want to allow in this statement. (e.g. `lambda:InvokeFunction`) 114 * `function_name` - (Required) Name of the Lambda function whose resource policy you are updating 115 * `principal` - (Required) The principal who is getting this permission. 116 e.g. `s3.amazonaws.com`, an AWS account ID, or any valid AWS service principal 117 such as `events.amazonaws.com` or `sns.amazonaws.com`. 118 * `statement_id` - (Required) A unique statement identifier. 119 * `qualifier` - (Optional) Query parameter to specify function version or alias name. 120 The permission will then apply to the specific qualified ARN. 121 e.g. `arn:aws:lambda:aws-region:acct-id:function:function-name:2` 122 * `source_account` - (Optional) The AWS account ID (without a hyphen) of the source owner. 123 * `source_arn` - (Optional) When granting Amazon S3 or CloudWatch Events permission to 124 invoke your function, you should specify this field with the Amazon Resource Name (ARN) 125 for the S3 Bucket or CloudWatch Events Rule as its value. This ensures that only events 126 generated from the specified bucket or rule can invoke the function. 127 API Gateway ARNs have a unique structure described 128 [here](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html).