0

so im stuck for a couple of days with my automation in SAM.

Problem: I cant reproduce the things i did over the GUI in AWS to my SAM Template. I managed over the GUI:

  • Create an Event Bridge Rule which gets triggered when a new object is created
  • Target: The EC2 Instance, which runs a script that finds the new object and loads it on to the EC2 instance
  • Target (EC2 Instance) Runs a command to import the new object to the graphdatabase Neo4j

Here you can see on the screenshots how i configured my event Rule:

Event Bridge Rule

enter image description here

Target

enter image description here

also the code:

EventBridge Rule

{
  "source": ["aws.s3"],
  "detail-type": ["Object Created"],
  "detail": {
    "bucket": {
      "name": ["xxxx-xxxxtbucket-2xxxxx"]
    },
    "object": {
      "key": [{
        "suffix": "1_only_users.cql"
      }]
    }
  }
}

Target Commands on EC2

FILE=`aws s3api list-objects-v2 --bucket "fxxxxx-xxxxbucket-2xxxxxxx" --query 'reverse(sort_by(Contents[?contains(Key, \`1_only_users\`)], &LastModified))[:1].Key' --output=text`;aws s3 cp s3://fxxxxx-xxxxbucket-2xxxxxxx/$FILE .

Working Directory: /home/ubuntu/

Question So my Question is how can i achieve the same results and transfer that logic to YAML on my SAM Template File.

Thanks a lot !

I tried a lot of things e.g.

  EventRule:
    Type: AWS::Events::Rule
    Properties:
      EventPattern:
        source:
          - "aws.s3"
        detail-type:
          - "Object Created"
        detail:
          bucket:
            name:
              - "xxxxxx-bucket-d"
          object:
            key:
              - suffix: "2_only_tweets.cql"
      Targets:
        - Arn: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:document/AWS-RunShellScript"
          Id: SSMTARGETrun
          InputTransformer:
            InputTemplate: !Sub |
              {
                "commands": [
                  "aws s3 cp s3://xxxxxxxx/${EventBucket}/ .",
                  "PSEUDOCODE"
                ]
              }

I just tried some random commands But i get the error: Parameter RunCommandParameters is not valid for target SSMTARGETrun.

I also tried the following approach:

  MySSMDocumentWithTargetIDNEW:
    Type: AWS::SSM::Document
    Properties:
      Name: MySSMDocumentWithTargetIDNEW
      DocumentType: Command
      Content:
        schemaVersion: "2.2"
        description: "Command Document Example YAML Template"
        parameters:
          Message:
            type: "String"
            description: "Example"
            default: "Hello World two"
          InstanceId:
            type: String
            description: "(Required) The instance ID you want to run commands on."
            default: !Ref MyEC2Instance
        mainSteps:
          - action: "aws:runShellScript"
            name: "example"
            inputs:
              runCommand:
                - "echo {{Message}}"
              instanceId: '{{InstanceId}}'
              workingDirectory: "/"
              targets:
                - key: InstanceIds
                  values:
                    - !Ref MyEC2Instance

But the last one is not directly bound to an event rule. So i stopped trying this approach. Can somebody help me and tell me which approach to follow? Or give me some completely new advice ?

Sampath
  • 810
  • 2
  • 2
  • 13

0 Answers0