【问题标题】:Is it a possible to call a lambda in different account from the cloudformation one?是否可以在与 cloudformation 不同的帐户中调用 lambda?
【发布时间】:2018-08-08 13:08:22
【问题描述】:

我有一个附加此政策的帐户上的 lambda:

{
  "Sid": "Id-123",
  "Effect": "Allow",
  "Principal": { "AWS": "arn:aws:iam::115333656057:root"},
  "Action": "lambda:InvokeFunction",
  "Resource": "arn:aws:lambda:eu-central-1:260143830488:function:CentralInstanceScheduler-InstanceSchedulerMain"
}

当我从帐户 115333656057 创建堆栈时,我的用户尝试执行 lambda,我收到此错误:

  User: arn:aws:iam::115333656057:user/uguesm is not authorized to perform: lambda:InvokeFunction on resource: arn:aws:lambda:eu-central-1:260143830488:function:CentralizedInstanceScheduler-InstanceSchedulerMain

我做错了什么?

【问题讨论】:

    标签: amazon-web-services lambda amazon-cloudformation policy


    【解决方案1】:

    您可以使用Lambda resource-based policy 来完成。这是直接存在于 Lambda 上的策略。使用基于资源的策略来避免需要创建额外的角色和使用 STS::AssumeRole。

    在 CDK 中,它看起来像这样:

            account = 123456789012
            lambda_.CfnPermission(
                scope,
                f"XAccountInvocation{account}",
                action="lambda:InvokeFunction",
                function_name=handler.function_name,
                principal=f"arn:aws:iam::{account}:root",
            )
    

    【讨论】:

      【解决方案2】:

      在账户 260143830488 中 - 编辑您的角色以将策略添加到 InvokeFunction 并为另一个账户添加信任策略。

      权限:

      {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Effect": "Allow",
            "Action": "lambda:InvokeFunction",
            "Resource": "arn:aws:lambda:eu-central-1:260143830488:function:CentralInstanceScheduler-InstanceSchedulerMain"
          },
        ]
      }
      

      信任关系政策:

      {
        "Sid": "Id-123",
        "Effect": "Allow",
        "Principal": { "AWS": "arn:aws:iam::115333656057:role/<lambda-role>"},
        "Action": "sts:AssumeRole",
      }
      

      在帐户 115333656057 中 - 创建一个 lambda 执行角色到 AssumeRole

      权限:

      {
        "Version": "2012-10-17",
        "Statement": {
          "Effect": "Allow",
          "Action": "sts:AssumeRole",
          "Resource": "arn:aws:iam::260143830488:role/<RoleName>"
        }
      }
      

      信任关系政策:

      {
        "Version": "2012-10-17",
        "Statement": {
          "Effect": "Allow",
          "Principal": {"Service": "lambda.amazonaws.com"},
          "Action": "sts:AssumeRole"
        }
      }
      

      【讨论】:

      • 我创建了所有描述的资源,但我省略了一些细节。 lambda 函数是由 CloudFormation 在 aws CustomResource "Resources": { "OfficehoursSwitzerland": { "Type": "Custom::ServiceInstanceSchedule", "Properties": { "ServiceToken": "arn:aws:lambda:eu-central-1:260143830488:function:CentralInstanceScheduler" , 上从账户 115333656057 触发的
      • 我猜你在创建堆栈时没有设置角色?您确定在创建堆栈时传递角色吗? docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
      猜你喜欢
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      相关资源
      最近更新 更多