gpt4 book ai didi

amazon-web-services - AWS 云信息 :Template validation error Role and policy

转载 作者:行者123 更新时间:2023-12-03 07:39:20 24 4
gpt4 key购买 nike

我是 cloudformation 的新手,正在尝试创建一个模板,该模板可以为我的 lambda 函数创建执行角色和关联策略。

AWSTemplateFormatVersion: 2010-09-09
Description: AWS CloudFormation Template for creating iam role for SSM lambda
Parameters:
rolename:
Type: String
Description: The name of the iam role for SSM Lambda
Default: SSM_lambda_role
policyname:
Type: String
Description: pcluster lambda iam policy for SSM Lambda
Default: SSM_lambda_policy
Resources:
ssmlambdarole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Sub '${rolename}'
Description: iam role for ssm lambda role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- !Sub 'arn:aws:iam::${AWS::AccountId}:policy/${policyname}'
ssmlambdapolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: !Sub '${policyname}'
Description: The name of the iam role for SSM Lambda
Path: '/'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- logs:CreateLogGroup
Resource: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
Effect: Allow
Sid: CloudWatchLogsPolicy
- Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${policyname}:*
Effect: Allow
Sid: CloudWatchLogsPolicy
- Action:
- ssm:Describe*
- ssm:Get*
- ssm:List*
Resource: "*"
Effect: Allow

如果我首先在上述模板中定义角色,我会在堆栈创建过程中收到错误,指出未找到策略;如果我首先按上述顺序创建策略,则会不断收到验证错误。谁能告诉我我哪里错了。

最佳答案

有一个属性可以帮助实现这一目标:DependsOn ,

但更好的方法是使用 - !Ref ssmlambdapolicy 而不是 - !Sub 'arn:aws:iam::${AWS::AccountId}:policy/${policyname }'

在每种情况下,它都会在资源之间建立依赖关系。由于 AWS 将能够识别资源创建顺序 - 您没有使用其中任何一个,因此 AWS 首先尝试创建一个角色(或策略,具体取决于模板中的顺序),并附加一个不支持的策略。还不存在。

验证错误是由于您在策略声明中遗漏了!sub


顺便说一句,我强烈建议在 CFN 文档中寻找帮助 - 有时有一个带有 use-case examples 的部分.

关于amazon-web-services - AWS 云信息 :Template validation error Role and policy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73657851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com