There are more AWS SDK examples available in the AWS Doc SDK Examples
Amazon Data Lifecycle Manager examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with Amazon Data Lifecycle Manager.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use create-default-role.
- AWS CLI
-
To create the required IAM role for Amazon DLM
The following
create-default-roleexample creates the AWSDataLifecycleManagerDefaultRole default role for managing snapshots.aws dlm create-default-role \ --resource-typesnapshotThis command produces no output.
For more information, see Default service roles for Amazon Data Lifecycle Manager in the Amazon Elastic Compute Cloud User Guide.
-
For API details, see CreateDefaultRole
in AWS CLI Command Reference.
-
The following code example shows how to use create-lifecycle-policy.
- AWS CLI
-
To create a lifecycle policy
The following
create-lifecycle-policyexample creates a lifecycle policy that creates a daily snapshot of volumes at the specified time. The specified tags are added to the snapshots, and tags are also copied from the volume and added to the snapshots. If creating a new snapshot exceeds the specified maximum count, the oldest snapshot is deleted.aws dlm create-lifecycle-policy \ --description"My first policy"\ --stateENABLED\ --execution-role-arnarn:aws:iam::12345678910:role/AWSDataLifecycleManagerDefaultRole\ --policy-detailsfile://policyDetails.jsonContents of
policyDetails.json:{ "ResourceTypes": [ "VOLUME" ], "TargetTags": [ { "Key": "costCenter", "Value": "115" } ], "Schedules":[ { "Name": "DailySnapshots", "CopyTags": true, "TagsToAdd": [ { "Key": "type", "Value": "myDailySnapshot" } ], "CreateRule": { "Interval": 24, "IntervalUnit": "HOURS", "Times": [ "03:00" ] }, "RetainRule": { "Count":5 } } ] }Output:
{ "PolicyId": "policy-0123456789abcdef0" }For more information, see Create Amazon Data Lifecycle Manager custom policy for EBS snapshots in the Amazon EBS User Guide.
-
For API details, see CreateLifecyclePolicy
in AWS CLI Command Reference.
-
The following code example shows how to use delete-lifecycle-policy.
- AWS CLI
-
To delete a lifecycle policy
The following
delete-lifecycle-policyexample deletes the specified lifecycle policy.aws dlm delete-lifecycle-policy \ --policy-idpolicy-0123456789abcdef0This command produces no output.
For more information, see Delete Amazon Data Lifecycle Manager policies in the Amazon EBS User Guide.
-
For API details, see DeleteLifecyclePolicy
in AWS CLI Command Reference.
-
The following code example shows how to use get-lifecycle-policies.
- AWS CLI
-
To get a summary of your lifecycle policies
The following
get-lifecycle-policiesexample lists all of your lifecycle policies.aws dlm get-lifecycle-policiesOutput:
{ "Policies": [ { "PolicyId": "policy-0123456789abcdef0", "Description": "My first policy", "State": "ENABLED" } ] }For more information, see Automate backups with Amazon Data Lifecycle Manager in the Amazon EBS User Guide.
-
For API details, see GetLifecyclePolicies
in AWS CLI Command Reference.
-
The following code example shows how to use get-lifecycle-policy.
- AWS CLI
-
To describe a lifecycle policy
The following
get-lifecycle-policyexample displays details for the specified lifecycle policy.aws dlm get-lifecycle-policy \ --policy-idpolicy-0123456789abcdef0Output:
{ "Policy": { "PolicyId": "policy-0123456789abcdef0", "Description": "My policy", "State": "ENABLED", "ExecutionRoleArn": "arn:aws:iam::123456789012:role/AWSDataLifecycleManagerDefaultRole", "DateCreated": "2019-08-08T17:45:42Z", "DateModified": "2019-08-08T17:45:42Z", "PolicyDetails": { "PolicyType": "EBS_SNAPSHOT_MANAGEMENT", "ResourceTypes": [ "VOLUME" ], "TargetTags": [ { "Key": "costCenter", "Value": "115" } ], "Schedules": [ { "Name": "DailySnapshots", "CopyTags": true, "TagsToAdd": [ { "Key": "type", "Value": "myDailySnapshot" } ], "CreateRule": { "Interval": 24, "IntervalUnit": "HOURS", "Times": [ "03:00" ] }, "RetainRule": { "Count": 5 } } ] } } }For more information, see Automate backups with Amazon Data Lifecycle Manager in the Amazon EBS User Guide.
-
For API details, see GetLifecyclePolicy
in AWS CLI Command Reference.
-
The following code example shows how to use update-lifecycle-policy.
- AWS CLI
-
Example 1: To enable a lifecycle policy
The following
update-lifecycle-policyexample enables the specified lifecycle policy.aws dlm update-lifecycle-policy \ --policy-idpolicy-0123456789abcdef0\ --stateENABLEDThis command produces no output.
For more information, see Modify Amazon Data Lifecycle Manager policies in the Amazon EBS User Guide.
Example 2: To disable a lifecycle policy
The following
update-lifecycle-policyexample disables the specified lifecycle policy.aws dlm update-lifecycle-policy \ --policy-idpolicy-0123456789abcdef0\ --stateDISABLEDThis command produces no output.
For more information, see Modify Amazon Data Lifecycle Manager policies in the Amazon EBS User Guide.
Example 3: To update the details for lifecycle policy
The following
update-lifecycle-policyexample updates the target tags for the specified lifecycle policy.aws dlm update-lifecycle-policy \ --policy-idpolicy-0123456789abcdef0\ --policy-detailsfile://policyDetails.jsonThis command produces no output.
Other details not referenced in the file are not changed by the command.
Contents of
policyDetails.json:{ "TargetTags": [ { "Key": "costCenter", "Value": "120" }, { "Key": "project", "Value": "lima" } ] }For more information, see Modify Amazon Data Lifecycle Manager policies in the Amazon EBS User Guide.
-
For API details, see UpdateLifecyclePolicy
in AWS CLI Command Reference.
-