awsdocs / amazon-cloudwatch-events-user-guide

The open source version of the Amazon CloudWatch Events User Guide. You can submit feedback & requests for changes by submitting issues in this repo or by making proposed changes & submitting a pull request.
Other
29 stars 28 forks source link

Incomplete turorial: Run an Amazon ECS Task When a File is Uploaded to an Amazon S3 Bucket #9

Closed qzysw123456 closed 1 year ago

qzysw123456 commented 4 years ago

https://github.com/awsdocs/amazon-cloudwatch-events-user-guide/blob/master/doc_source/CloudWatch-Events-tutorial-ECS.md

It mentioned everything but Cloudtrail. From my experience it doesn't work otherwise you correctly setting up cloud trail and log object level API calls to a logGroup

ajdinm commented 3 years ago

Additionally, it would be useful to see how to read event metadata (e.g. bucket/key of the file uploaded; if possible) from inside the container. I found 0 resources about this online, maybe there is some ENV inside container that isn't documenented?

iamdakshay commented 3 years ago

@ajdinm ,

  1. While creating rule target, InputTransformer attibute can be used to pass meatdata to Target.

  2. For ECS task as a target, AWS console do not provide option to provide InputTransformer attribute. We can specify it while creating resource via CloudFormation/Terraform or using AWS CLI.

  3. Please find following AWS CLI command to ECS task target to existing rule and provide metadata.

    aws events put-targets --rule "name of rule" --targets '{
    "Id": "9",
    "Arn": "arn of ECS cluster",
    "RoleArn": "arn of role to invoke ECS task",
    "EcsParameters": {
        "TaskDefinitionArn": "arn of task definition",
        "TaskCount": 1,
        "LaunchType": "FARGATE",
        "NetworkConfiguration": {
            "awsvpcConfiguration": {
                "Subnets": [
                    "id of subnet"
                ],
                "AssignPublicIp": "DISABLED"
            }
        },
        "Group": "demo"
    },
    "InputTransformer": {
        "InputPathsMap": {
            "s3-bucket": "$.detail.requestParameters.bucketName",
            "s3-key": "$.detail.requestParameters.key"
        },
        "InputTemplate": "{
            \"containerOverrides\": [
                {
                    \"name\": \"name of container in task def\",
                    \"environment\": [
                        {
                            \"name\": \"s3-bucket\",
                            \"value\": \"<s3-bucket>\"
                        },
                        {
                            \"name\": \"s3-key\",
                            \"value\": \"<s3-key>\"
                        }
                    ]
                }
            ]
        }"
    }
    }'
  4. Metadata will be passed to container as environment variable. I am using dotnet core based container hence I am accessing those variables as follow. Console.WriteLine($"bucket name: {Environment.GetEnvironmentVariable("s3-bucket")}"); Console.WriteLine($"file name: {Environment.GetEnvironmentVariable("s3-key")}");

ajdinm commented 3 years ago

This is helpful, thank you @iamdakshay .

joshbean commented 1 year ago

Closing this issue or pull request in advance of archiving this repo. For more information about the decision to archive this repo (and others in the 'awsdocs' org), see the announcement on the AWS News Blog.