Skip to content

boto3

SSO connection

Use boto3 with configured sso profile

import boto3

path = "preprocess/output/xxxxxxxx/data.pkl"
des = "./test_s3/data.pkl"
session = boto3.Session(profile_name='test-xxxx')
s3 = session.resource("s3")

for bucket in s3.buckets.all():
    print(bucket.name)

CloudWatch

  • List alarm

    import boto3
    
    # Create CloudWatch client
    session = boto3.Session(profile_name='test-xxxx')
    cloudwatch = session.client("cloudwatch")
    
    # List alarms of insufficient data through the pagination interface
    paginator = cloudwatch.get_paginator('describe_alarms')
    all_res = []
    for response in paginator.paginate(StateValue='OK'):
        # print(response['MetricAlarms'])
        all_res.append(response['MetricAlarms'])
    print(len(all_res))
    
  • Get alarms detail

    import boto3
    
    # Create CloudWatch client
    session = boto3.Session(profile_name='test-xxxx')
    cloudwatch = session.client("cloudwatch")
    
    response = cloudwatch.describe_alarms(
        AlarmNames=[
            'Keith - Test HTTP 5xx API ALB',
        ],
        # AlarmNamePrefix='string',
        # AlarmTypes=[
        #     'CompositeAlarm'|'MetricAlarm',
        # ],
        # ChildrenOfAlarmName='string',
        # ParentsOfAlarmName='string',
        # StateValue='OK'|'ALARM'|'INSUFFICIENT_DATA',
        # ActionPrefix='string',
        # MaxRecords=123,
        # NextToken='string'
    )
    pp(response)
    
  • Create Alarm

    import boto3
    
    # Create CloudWatch client
    session = boto3.Session(profile_name='test-xxxxx')
    cloudwatch = session.client("cloudwatch")
    
    # Create alarm
    cloudwatch.put_metric_alarm(
        AlarmName='Keith - Test HTTP 5xx inference ALB',
        AlarmActions= ['arn:aws:sns:xxxxx'],
        ComparisonOperator='GreaterThanThreshold',
        MetricName='HTTPCode_Target_5XX_Count',
        Namespace='AWS/ApplicationELB',
        Period=60,
        Statistic='Sum',
        Threshold=0.0,
        DatapointsToAlarm=1,
        ActionsEnabled=True,
        AlarmDescription='Just test. Fire alarm if 5xx http code.',
        Dimensions=[{
            'Name': 'LoadBalancer',
            'Value': 'app/xxxx-alb/xxxxxxxx'
        }],
        EvaluationPeriods=1,
        TreatMissingData='notBreaching'
    )
    

    S3

Comments