AWS: Resource Identification
Tags are used in every AWS resource we use it for the proper identification. To identify the resources which are only used in some particular environment for that we use environment tag. These tags can give us the information about the resource for example whether it is in production environment or test environment. We can identify that by environment tags.
Boto3 is a module which is used for the API references of the AWS services. We can implement it using python as it is a faster and less complex way to handle but this module is also available across programmatic languages. There are two or three ways we implement Boto3.
By client, resources or paginator.
We can use used clients but to take individual resources we can use the Boto3 resource object. We can find boto3 documentation in this link.
Link - https://boto3.amazonaws.com/v1/documentation/api/latest/index.html
Let's take an example of boto3 scripting.
Case: to print the EC2 instance present in roles accounts–
import boto3
client = boto3.client('ec2')
response = client.describe_instances(
Filters=[// we can use filters specific Instances it is totally optional
{
'Name': 'string',
'Values': [
'string',
]
},
]
)
Case study: EC2 tags ENV
In every firm in there standard guidelines they mandatorises the use of unique resource tags , environment tags and AppName tags. They does that because when if there is an cyber attack the first step they follow is to find the unique resource id which resources are impacted and using the ENV tags they figure it out the impacted environment. They also mandatorise this tags to give an unique name. Altogether it can make up a unique identification for a particular resources. Let's follow up with EC2 instance environment tags.
– First to find whether the EC2 tags is present or not.
import boto3
client = boto3.client('ec2')
response = client.describe_tags(Filters=[ {'Name': 'string', 'Values': ['string'])}
// response is our Output for a particular EC2 instance tags
Present = True
for x in response['tags']:
if 'ENV' in x:
Present = True
Break
else:
Present = False
-- Secondly after checking add or print the output.
if present:
// two cases arises one the owner world provide the ENV tag value
// Or we going to retrieve it by the EC2 instance name itself
// it is retrieved from the name and is stored in tags
// creat_tags is used for creating tags or modifying the current tags
Updated_response = client.create_tags(Resources=[ 'string', ], Tags=tags ])
print(Updated_response)
//Output
else:
print("Tags is already present")
Will discuss the Security groups case study and continue with boto3 documentation and how to implement the Python Script next week's Blog.
Thank You
Comments
Post a Comment