amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Recommended approach for attaching custom policies to functions

Open timkuilman opened this issue 7 years ago • 10 comments

** Which Category is your question related to? ** api's / functions

** What AWS Services are you utilizing? ** SNS, SQS, et cetera

** Provide additional details e.g. code snippets **

Example of a default cloudformation template

	"AWSTemplateFormatVersion": "2010-09-09",
	"Description": "Lambda resource stack creation using Amplify CLI",
	"Parameters": {
		"env": {
			"Type": "String"
		}
	},
	"Conditions": {
		"ShouldNotCreateEnvResources": {
			"Fn::Equals": [
				{
					"Ref": "env"
				},
				"NONE"
			]
		}
	},
	"Resources": {
		"LambdaFunction": {
			"Type": "AWS::Lambda::Function",
			"Metadata": {
				"aws:asset:path": "./src",
				"aws:asset:property": "Code"
			},
			"Properties": {
				"Handler": "index.handler",
				"FunctionName": {
					"Fn::If": [
						"ShouldNotCreateEnvResources",
						"functionName",
						{
							"Fn::Join": [
								"",
								[
									"functionName",
									"-",
									{
										"Ref": "env"
									}
								]
							]
						}
					]
				},
				"Environment": {
					"Variables": {
						"ENV": {
							"Ref": "env"
						}
					}
				},
				"Role": {
					"Fn::GetAtt": [
						"LambdaExecutionRole",
						"Arn"
					]
				},
				"Runtime": "nodejs8.10",
				"Timeout": "25",
				"Code": {
					"S3Bucket": "projectName-20190225141031-deployment",
					"S3Key": "amplify-builds/functionName-1553669058-latest-build.zip"
				}
			}
		},
		"LambdaExecutionRole": {
			"Type": "AWS::IAM::Role",
			"Properties": {
				"RoleName": {
					"Fn::If": [
						"ShouldNotCreateEnvResources",
						"projectnameLambdaRoled89576a3",
						{
							"Fn::Join": [
								"",
								[
									"projectNameLambdaRoled89576a3",
									"-",
									{
										"Ref": "env"
									}
								]
							]
						}
					]
				},
				"AssumeRolePolicyDocument": {
					"Version": "2012-10-17",
					"Statement": [
						{
							"Effect": "Allow",
							"Principal": {
								"Service": [
									"lambda.amazonaws.com"
								]
							},
							"Action": [
								"sts:AssumeRole"
							]
						}
					]
				}
			}
		},
		"lambdaexecutionpolicy": {
			"DependsOn": [
				"LambdaExecutionRole"
			],
			"Type": "AWS::IAM::Policy",
			"Properties": {
				"PolicyName": "lambda-execution-policy",
				"Roles": [
					{
						"Ref": "LambdaExecutionRole"
					}
				],
				"PolicyDocument": {
					"Version": "2012-10-17",
					"Statement": [
						{
							"Effect": "Allow",
							"Action": [
								"logs:CreateLogGroup",
								"logs:CreateLogStream",
								"logs:PutLogEvents"
							],
							"Resource": {
								"Fn::Sub": [
									"arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*",
									{
										"region": {
											"Ref": "AWS::Region"
										},
										"account": {
											"Ref": "AWS::AccountId"
										},
										"lambda": {
											"Ref": "LambdaFunction"
										}
									}
								]
							}
						}
					]
				}
			}
		}
	},
	"Outputs": {
		"Name": {
			"Value": {
				"Ref": "LambdaFunction"
			}
		},
		"Arn": {
			"Value": {
				"Fn::GetAtt": [
					"LambdaFunction",
					"Arn"
				]
			}
		},
		"Region": {
			"Value": {
				"Ref": "AWS::Region"
			}
		}
	}
}

Each cloud function that we create via the Amplify CLI has it's own functionName-cloudformation-template.json file. I am assuming that this is the best place to attach additional IAM policies. Each function has by default a reference to the 'lambda execution policy' which is defined as an inline policy.

Is this the best place to give the function access to other AWS policies like SNS and SQS? I might prefer to add references to my own custom managed policies. How would I do that?

I would like to hear more opinions about this. Perhaps we can improve the Amplify documentation too?

timkuilman avatar Mar 27 '19 07:03 timkuilman

Yes, that's where you would add custom IAM Policies for your lambda function. I'll add a documentation label to this, so that we can track this for our documentation tasks.

kaustavghosh06 avatar Mar 29 '19 19:03 kaustavghosh06

So only inline policies are possible? Is there any way to refere to arns of managed policies?

timkuilman avatar Mar 30 '19 08:03 timkuilman

Hi, Im having similar issues. In my case I want to add a cognito policy. How can I ref the user Pool ID in this function? For now I have it to all (*) resources.. not ideal. I added this to my function generated template "lambdaexecutionpolicy": { "DependsOn": [ "LambdaExecutionRole" ], "Type": "AWS::IAM::Policy", "Properties": { "PolicyName": "lambda-execution-policy", "Roles": [ { "Ref": "LambdaExecutionRole" } ], "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Sid": "logs", "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": { "Fn::Sub": [ "arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*", { "region": { "Ref": "AWS::Region" }, "account": { "Ref": "AWS::AccountId" }, "lambda": { "Ref": "LambdaFunction" } } ] } }, { "Sid": "cognito", "Effect": "Allow", "Action": [ "cognito-idp: CreateGroup" ], "Resource": "*" } ] } } }

Any advice appreciated!

sergiorodriguez82 avatar May 15 '19 16:05 sergiorodriguez82

Today we released an updated flow as a part of the functions category to pass resource identifiers like the cognito userpool ID(generated and managed using Amplify) to a lambda function as environment variables and also populate the corresponding lambda execution role to access these resources. You can install the latest version of the CLI and go through the amplify update function flow to update your exisiting functions to access your Amplify generated resources in the project.

kaustavghosh06 avatar May 30 '19 05:05 kaustavghosh06

@kaustavghosh06 the cloudformation file functionName-cloudformation-template.json gets autogenerated on every push. Modifying it to add a resource policy is not really that feasible. Every time I change anything in the function, I have to update the cloudformation-template file...this limitation means that you cant write a custom policy to, say, publish to SQS. This severely restricts the type of functionality being built with amplify functions.

nibab avatar Apr 28 '20 14:04 nibab

@kaustavghosh06 the cloudformation file functionName-cloudformation-template.json gets autogenerated on every push. Modifying it to add a resource policy is not really that feasible. Every time I change anything in the function, I have to update the cloudformation-template file...this limitation means that you cant write a custom policy to, say, publish to SQS. This severely restricts the type of functionality being built with amplify functions.

Is this actually the case. I'm in exactly this scenario, I'd like to be able to send SMS messages based on a scheduled Lambda function deployed with Amplify. How would you recommend I add the appropriate permission to do this?

My initial thought was to use IAM Policies... but based on what I'm reading above here there doesn't seem to be an easy way to do this for resources outside of Amplify?

Is the best bet to use Environment Variables and pass in some session credentials for an IAM user which has the appropriate permissions?

Mentioum avatar Jul 04 '20 00:07 Mentioum

@kaustavghosh06 I think you didn't get the problem here.

The thing is not to access cognito, is to allow actions that are outside Amplify. I think you shouldn't close the issue.

ignaciolarranaga avatar Aug 21 '20 00:08 ignaciolarranaga

I'm having this same complication - not being able to add a custom policy to a resource. I can't see a relevant answer for this issue here, unless there has been an update in the documentation. If there has could it be linked here and if it hasn't why has this issue been closed?

johnEthicalTechnology avatar Oct 06 '20 10:10 johnEthicalTechnology

@kaustavghosh06 the cloudformation file functionName-cloudformation-template.json gets autogenerated on every push. Modifying it to add a resource policy is not really that feasible. Every time I change anything in the function, I have to update the cloudformation-template file...this limitation means that you cant write a custom policy to, say, publish to SQS. This severely restricts the type of functionality being built with amplify functions.

Is this actually the case. I'm in exactly this scenario, I'd like to be able to send SMS messages based on a scheduled Lambda function deployed with Amplify. How would you recommend I add the appropriate permission to do this?

My initial thought was to use IAM Policies... but based on what I'm reading above here there doesn't seem to be an easy way to do this for resources outside of Amplify?

Is the best bet to use Environment Variables and pass in some session credentials for an IAM user which has the appropriate permissions?

the cloudformation file functionName-cloudformation-template.json gets autogenerated on every push.

-> no this is not true. The file will be regenerated when you update your function by running the amplify function update command for that specific function. When updating code, or changing the functionName-cloudformation-template.json file, your changes will be kept after doing a push.

Is the best bet to use Environment Variables and pass in some session credentials for an IAM user which has the appropriate permissions?

-> Adding env vars would also require a change to functionName-cloudformation-template.json. So if your are going to do that anyway, I would directly attach the correct policy to the lambda function in functionName-cloudformation-template.json

Nxtra avatar Nov 22 '20 17:11 Nxtra

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 25 '21 13:05 github-actions[bot]