How to change the lambda function name for each run while rotating secrets

0

We 've a requirement to rotate the secrets for RDS MySQL. we are following the steps mentioned in the documentation https://docs.aws.amazon.com/secretsmanager/latest/userguide/reference_available-rotation-templates.html#sar-template-mysql-singleuser

Role, Lambda function permission get created and the secrets are rotated as well. when i use the code /template second time, Lambda function name is not changed and it causes the stack to fail . Is there a way to generate unique lambda function every time to avoid stack failure. code snippet : Transform: AWS::SecretsManager-2020-07-23 ... .... MySecretRotationSchedule: Type: AWS::SecretsManager::RotationSchedule
Properties: SecretId: !Ref Xyz HostedRotationLambda: RotationType: MySQLSingleUser
RotationRules: AutomaticallyAfterDays: 30

AWS
已提問 6 個月前檢視次數 171 次
1 個回答
1

Hello.

Use !Sub or !Join to Construct Unique Names: These functions allow you to concatenate strings and include dynamic elements like stack name or unique IDs. Incorporate AWS::StackName and/or AWS::Region: Using these pseudo parameters ensures that your Lambda function name is unique per stack and region.

For example:

Resources:
  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      # Other required properties like Code, Handler, Role, Runtime
      FunctionName: !Sub 
        - "${StackName}-${AWS::Region}-${UniqueID}-RotationLambda"
        - StackName: !Ref AWS::StackName
          UniqueID: !Ref UniqueResource # Replace with a unique resource in your template

  MySecretRotationSchedule:
    Type: AWS::SecretsManager::RotationSchedule
    Properties: 
      SecretId: !Ref Xyz
      HostedRotationLambda:
        RotationType: MySQLSingleUser
        RotationLambdaName: !Ref MyLambdaFunction
      RotationRules:
        AutomaticallyAfterDays: 30

Regards, Andrii

profile picture
專家
已回答 6 個月前
profile picture
專家
已審閱 2 天前
profile picture
專家
已審閱 1 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南