How to schedule automatic EBS snapshots using CloudWatch events using the CLI?

I am trying to recreate the procedure in Tutorial: EBS Automated Snapshot Schedule Using CloudWatch Events using the AWS CLI. I almost finished with one step: setting AWS permissions (step 9).

Configure AWS Permissions Using the AWS Console

If I do all the other steps using the AWS CLI and step 9 using the AWS console, everything works fine - snapshots are taken periodically. I was even able to create a role that is identical to the one that was manually created in step 9 (using "aws iam create-role" and "aws iam put-role-policy"), but did not apply it to my rule.

Is there a way to set AWS permissions for a CloudWatch rule using the AWS CLI?

EDIT: Let me clarify this. I do the following:

$ aws iam create-role --region <my_region> \
  --role-name "MyRoleForThisRule" \
  --assume-role-policy-document file://<my_role_policy_document>.json
$ aws iam put-role-policy --role-name "MyRoleForThisRule" --policy-name "MyRolePolicyForThisRule" --policy-document file://<my_policy_document>.json
$ aws events put-rule --region <my_region> --name "Snapshot_EBS_Rule" \
  --schedule-expression "cron(0 0 * * ? *)" \
  --role-arn arn:aws:iam::<my_aws_account_id>:role/MyRoleForThisRule
$ aws events put-targets --region <my_region> \
  --rule "Snapshot_EBS_Rule" \
  --targets '{"Input": "\"arn:aws:ec2:<my_region>:<my_aws_account_id>:volume/<my_ebs_volume_id>\"", "Id": "Snapshot_EBS_Target", "Arn": "arn:aws:automation:<my_region>:<my_aws_account_id>:action/EBSCreateSnapshot/EBSCreateSnapshot_MyData"}'

As a result:

  • A CloudWatch rule is created and assigned with the appropriate purpose and role,
  • AWS permissions are NOT specified as shown in this screenshot
  • snapshots are NOT created unless I manually set AWS rights through the AWS console.

How can I set AWS permissions using the AWS CLI?

+4
source share
1 answer

The AWS CLI has an events parameter to manage cloud time events.

,

aws events put-rule --name "EBSSnapshotTaken" \
    --event-pattern "{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EBS Snapshot Notification\"],\"detail\":{\"event\":\"createSnapshot\", \"result\":\"succeeded\",\"source\":\"<arn path of the volume id being backup\""}}" \
    --role-arn "arn:aws:iam::123456789012:role/MyRoleForThisRule"

Cloudwatch

+2

Source: https://habr.com/ru/post/1680104/


All Articles