I have drafted a CF template for automating windows ec2 patching, unfortunatly ended with the below error. error details are given in description. kindly have a look and help me to fix it.

0

"Resource handler returned message: "Invalid request provided: Invalid document provided (Service: Ssm, Status Code: 400, Request ID: 723907ad-4da8-4cfd-8d32-f3e0e37efe01)" (RequestToken: 389c0ddf-ee7a-154a-d2a5-f70a7ccc4677, HandlerErrorCode: InvalidRequest)"

AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  InstanceId:
    Type: "String"
    Description: "ID of the EC2 instance to patch"
  SnsTopicName:
    Type: "String"
    Description: "Name of the SNS topic for notifications"

Resources:
  PatchingDocument:
    Type: "AWS::SSM::Document"
    Properties:
      DocumentType: "Command"
      Content:
        schemaVersion: "2.2"
        description: "WindowsPatch"
        mainSteps:
          - action: "aws:runPowerShellScript"
            name: "ScanAndInstallPatches"
            inputs:
              runCommand:
                - "& { Install-WindowsUpdate -AcceptAll -AutoReboot }"

  PatchingAssociation:
    Type: "AWS::SSM::Association"
    Properties:
      AssociationName: "WindowsPatchAssociation"
      Targets:
        - Key: "instanceids"
          Values:
            - Ref: "InstanceId"
      DocumentVersion: "1"
      Name: "AWS-InstallPatches"
      ScheduleExpression: "rate(1 day)"  # Modify the schedule as needed

  SnsTopic:
    Type: "AWS::SNS::Topic"
    Properties:
      DisplayName: "WindowsPatchNotifications"
      TopicName: !Ref "SnsTopicName"

  SnsSubscription:
    Type: "AWS::SNS::Subscription"
    Properties:
      Protocol: "email"  # You can change this to other protocols like "sms", "lambda", etc.
      TopicArn: !Ref "SnsTopic"
      Endpoint: "your-email@example.com"  # Replace with your email address

  SnsPermission:
    Type: "AWS::SNS::TopicPolicy"
    Properties:
      Topics:
        - Ref: "SnsTopic"
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service: "ssm.amazonaws.com"
            Action: "SNS:Publish"
            Resource: !Ref "SnsTopic"

Manoj
질문됨 5달 전122회 조회
1개 답변
0

The "Invalid document provided" error indicates that the SSM document referenced for the association does not exist.

Looking at your CFN template, I see two possible reasons for the error:

  1. You are creating a Command document with name as "ScanAndInstallPatches" whereas in your Association, you are referencing a different document "AWS-InstallPatches". So, I would suggest reviewing the template again to make sure that correct document name is provided in Association.

  2. If a "DependsOn" Attribute or a Reference intrinsic function is not specified in CFN template, then CloudFormation will initiate the resource provisioning API calls simultaneously for both SSM document and SSM Association. If the calls were made simultaneously indeed, then the document resource was not yet created while the Association was getting created leading to the "Invalid Document" error.

You can update the Stack template so that the "AWS::SSM::Association" resource references the document name, when using Reference intrinsic function (1), CloudFormation will automatically take care of the internal dependency between the 2 resources.

PatchingAssociation:
    Type: "AWS::SSM::Association"
    Properties:
      AssociationName: "WindowsPatchAssociation"
      Targets:
        - Key: "instanceids"
          Values:
            - Ref: "InstanceId"
      DocumentVersion: "1"
      Name: !Ref "AWS-InstallPatches"
      ScheduleExpression: "rate(1 day)"  # Modify the schedule as needed

Reference:

(1) https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html

AWS
지원 엔지니어
Aamir_H
답변함 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠