To improve code reusability and manageability, DevOps engineers are modularizing their code.
Dependencies Inverse
Depth of Modules
How to pass parameters between modules
To improve code reusability and manageability, DevOps engineers are modularizing their code.
Dependencies Inverse
Depth of Modules
How to pass parameters between modules
data "aws_caller_identity" "current" {
$ aws cloudformation create-stack --stack-name test-vpc --template-body file://vpc/goweekend-vpc-1-dev.yaml --profile my profile --parameters ParameterKey=Environment,ParameterValue=goweekend-devops-esloyalty-vpc-1 ParameterKey=Infrastructure,ParameterValue= ParameterKey=VpcName,ParameterValue=goweekend-devops-esloyalty-vpc-1-prod
$ aws cloudformation delete-stack --stack-name test-vpc --profile my profile
Here's some more detailed information on what Client, Resource, and Session are all about.
Client:
Here's an example of client-level access to an S3 bucket's objects (at most 1000**):
import boto3
client = boto3.client('s3')
response = client.list_objects_v2(Bucket='mybucket')
for content in response['Contents']:
obj_dict = client.get_object(Bucket='mybucket', Key=content['Key'])
print(content['Key'], obj_dict['LastModified'])
** you would have to use a paginator, or implement your own loop, calling list_objects() repeatedly with a continuation marker if there were more than 1000.
Resource:
Reference:
https://stackoverflow.com/questions/42809096/difference-in-boto3-between-resource-client-and-session
Here's the equivalent example using resource-level access to an S3 bucket's objects (all):
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
for obj in bucket.objects.all():
print(obj.key, obj.last_modified)
Note that in this case you do not have to make a second API call to get the objects; they're available to you as a collection on the bucket. These collections of subresources are lazily-loaded.
You can see that the Resource
version of the code is much simpler, more compact, and has more capability (it does pagination for you). The Client
version of the code would actually be more complicated than shown above if you wanted to include pagination.
Session:
A useful resource to learn more about these boto3 concepts is the introductory re:Invent video.
In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line.
Note: Required options are generally considered bad form because users expect options to be optional, and thus they should be avoided when possible.
parser = argparse.ArgumentParser(description='Foo')
parser.add_argument('-o', '--output', help='Output file name', default='stdout')
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-i', '--input', help='Input file name', required=True)
parser.parse_args(['-h'])