AWS CLI:Lambda无法假定为该函数定义的角色

Aru*_*pta 6 security amazon-web-services aws-cli aws-lambda serverless-architecture

AWS CLI版本:

aws --version
aws-cli/1.11.21 Python/2.7.12 Darwin/15.3.0 botocore/1.4.78
Run Code Online (Sandbox Code Playgroud)

尝试创建Lambda函数并获取错误:

An error occurred (InvalidParameterValueException) when calling the CreateFunction operation: The role defined for the function cannot be assumed by Lambda.
Run Code Online (Sandbox Code Playgroud)

角色创建为:

aws iam create-role --role-name microrole --assume-role-policy-document file://./trust.json
Run Code Online (Sandbox Code Playgroud)

trust.json 是:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

政策附件如下:

aws iam put-role-policy --policy-document file://./policy.json --role-name microrole --policy-name micropolicy
Run Code Online (Sandbox Code Playgroud)

policy.json 是:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "apigateway:*"
      ],
      "Resource": "arn:aws:apigateway:*::/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "execute-api:Invoke"
      ],
      "Resource": "arn:aws:execute-api:*:*:*"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

等了[1][2]中提到的多分钟,但错误仍然没有消失.附加到该角色的策略和信任类似于使用Console创建Lambda函数时创建的默认角色.

完整步骤在https://github.com/arun-gupta/serverless/tree/master/aws/microservice中列出.

少了什么东西?

Aru*_*pta 1

Lambda 函数创建为:

aws lambda create-function \
--function-name MicroserviceGetAll \
--role arn:aws:iam::<act-id>:role/service-role/microRole \
--handler org.sample.serverless.aws.couchbase.BucketGetAll \
--zip-file fileb:///Users/arungupta/workspaces/serverless/aws/microservice/microservice-http-endpoint/target/microservice-http-endpoint-1.0-SNAPSHOT.jar \
--description "Microservice HTTP Endpoint - Get All" \
--runtime java8 \
--region us-west-1 \
--timeout 30 \
--memory-size 1024 \
--environment Variables={COUCHBASE_HOST=ec2-35-165-83-82.us-west-2.compute.amazonaws.com} \
--publish
Run Code Online (Sandbox Code Playgroud)

正确的命令是:

aws lambda create-function \
--function-name MicroserviceGetAll \
--role arn:aws:iam::<act-id>:role/microRole \
--handler org.sample.serverless.aws.couchbase.BucketGetAll \
--zip-file fileb:///Users/arungupta/workspaces/serverless/aws/microservice/microservice-http-endpoint/target/microservice-http-endpoint-1.0-SNAPSHOT.jar \
--description "Microservice HTTP Endpoint - Get All" \
--runtime java8 \
--region us-west-1 \
--timeout 30 \
--memory-size 1024 \
--environment Variables={COUCHBASE_HOST=ec2-35-165-83-82.us-west-2.compute.amazonaws.com} \
--publish
Run Code Online (Sandbox Code Playgroud)

不同之处在于角色被错误地指定为role/service-role/microRole而不是role/microRole