How to format date string to custom format in AWS SSM document

34 views Asked by At

I am trying to create AMI , then tag it, in tagging I wanted to use custom format , but result what i wanted in date value is not getting it

The SSM document I have:

description: Creates an AMI image from an EC2 instance
schemaVersion: '0.3'
assumeRole: '{{ AutomationAssumeRole }}'
parameters:
  InstanceId:
    type: AWS::EC2::Instance::Id
    description: The ID of the Amazon EC2 instance.
  TargetAmiName:
    type: String
    description: (Optional) The name of the new AMI that will be created. Default is a system-generated string including the source AMI id, and the creation time and date
    default: AMI_{{global:DATE_TIME}}
  NoReboot:
    type: Boolean
    description: (Optional) Do not reboot the instance before creating the image.
    default: true
  AutomationAssumeRole:
    type: AWS::IAM::Role::Arn
    description: (Optional) The ARN of the role that allows Automation to perform the actions on your behalf.
    default: arn:aws:iam::xxxxxxxxxxx:role/automation-dev
mainSteps:
  - name: createImage
    action: aws:createImage
    nextStep: RunPowerShell
    isEnd: false
    onFailure: Abort
    inputs:
      InstanceId: '{{ InstanceId }}'
      ImageName: '{{ TargetAmiName }}'
      NoReboot: '{{ NoReboot }}'
    outputs:
      - Name: CustomImageId
        Selector: $.ImageId
        Type: String
  - name: RunPowerShell
    action: aws:runCommand
    maxAttempts: 3
    nextStep: createTags
    isEnd: false
    inputs:
      DocumentName: AWS-RunPowerShellScript
      InstanceIds:
        - '{{ InstanceId }}'
      TimeoutSeconds: 300
      Parameters:
        commands:
          - '[System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.ShortDatePattern'
          - $formattedDate = (Get-Date).ToString("yyyy-MM-ddTH-mm")
    outputs:
      - Name: FormattedDate
        Selector: $.formattedDate
  - name: createTags
    action: aws:createTags
    maxAttempts: 3
    isEnd: true
    onFailure: Abort
    inputs:
      ResourceType: EC2
      ResourceIds:
        - '{{ createImage.CustomImageId }}'
      Tags:
        - Key: environment
          Value: dev
        - Key: backupdate
          Value: '{{ RunPowerShell.FormattedDate }}'

When I run this document, It creates image, while tagging date value key backupdate coming as "{RunPowerShell.FormattedDate}" like this...

basically I wanted value for backup date in tags as "yyyy-mm-dd HH-mm" like this

can you please suggest

0

There are 0 answers