추가

import json
import boto3

def lambda_handler(event, context):
    region_name = "ap-northeast-2"
    sg_id = "sg-xxxxx"
    from_port = 2222
    to_port = 2222
    protocol = "TCP"
    cidr = "1.1.1.1/32"
    description = "add by lambda"

    ec2 = boto3.client("ec2", region_name=region_name)

    response = ec2.authorize_security_group_ingress(
        GroupId=sg_id,
        IpPermissions=[
            {
                "FromPort": from_port,
                "ToPort": to_port,
                "IpProtocol": protocol,
                "IpRanges": [
                    {"CidrIp": cidr, "Description": description},
                ],
            },
        ],
    )

제거

import json
import boto3

def lambda_handler(event, context):
    region_name = "ap-northeast-2"
    sg_id = "sg-xxxxx"
    from_port = 2222
    to_port = 2222
    cidr = "1.1.1.1/32"

    ec2 = boto3.client("ec2", region_name=region_name)
    response = ec2.revoke_security_group_ingress(
        GroupId=sg_id, CidrIp=cidr, FromPort=from_port, ToPort=to_port, IpProtocol="tcp"
    )