如何通过其友好的名称或使用cloudformation从现有的Lambda函数在云上获得Rolename ?



我已经为RDS的旋转秘密创建了一个CFN模板。它由一个AWS hostdlambda组成,它调用一个自动生成的角色。我想给这个角色附加另一个策略。

rRotationLambdaDecryptPolicy:
Type: AWS::IAM::ManagedPolicy
DependsOn: rSecretRotationScheduleHostedRotationLambda
Properties:
Description: "Providing access to HostedLambda for decrypting KMS"
ManagedPolicyName: CustomedHostedLambdaKmsUserRolePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: AllowLambdaDecryptKMS
Effect: Allow
Action:
- kms:Decrypt
- kms:CreateGrant
Resource:
- !Sub arn:aws:kms:*:${AWS::AccountId}:key/*
Condition:
ForAnyValue:StringLike:
kms:ResourceAliases: alias/SecretsManager_KMSKey
Roles: <friendly rolename>

问题是我知道Lambda友好的名称及其Arn。需要找到链接到这个lambda的角色名,以便我可以将上面的策略附加到它(将它的友好名称添加到Roles)。

尝试将此附在

下面
Roles: 
Fn::Join:
- ""
- - '"'
- Fn::GetAtt:
- !Sub 'arn:aws:lambda::${AWS::AccountId}:function:SecretsManager-research-creds-rotation-lambda'
- Role
- '"'

注:-这里不能使用importvalue,因为嵌套堆栈是由AWS创建的,它的输出不包含export。

λ使用:-

rSecretRotationSchedule:
Type: AWS::SecretsManager::RotationSchedule
Properties:
SecretId:<SecretId>
HostedRotationLambda:
KmsKeyArn: <KmsKeyArn>
MasterSecretArn: <MasterSecretArn>
MasterSecretKmsKeyArn: <MasterSecretKmsKeyArn>
RotationType: PostgreSQLMultiUser
RotationLambdaName: SecretsManager-research-creds-rotation-lambda
VpcSecurityGroupIds: <VpcSecurityGroupIds>
VpcSubnetIds: <VpcSubnetIds>
RotationRules:
AutomaticallyAfterDays: 60

下面是我引用模板的链接:-https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-rotationschedule.html

对于普通的CFN,很可能无法做到这一点。您必须开发自定义资源。这将以新的lambda函数的形式出现,该函数将使用AWS SDK来查询主lambda函数的属性。一旦自定义资源找到角色名,它将返回到您的堆栈以供进一步使用。

最新更新