def put_function()

in apply/src/awsqs_kubernetes_resource/vpc.py [0:0]


def put_function(sess, cluster_name):
    eks = sess.client("eks")
    try:
        eks_vpc_config = eks.describe_cluster(name=cluster_name)["cluster"][
            "resourcesVpcConfig"
        ]
    except eks.exceptions.ResourceNotFoundException:
        raise exceptions.InvalidRequest(f"cluster with name {cluster_name} not found")
    ec2 = sess.client("ec2")
    internal_subnets = [
        s["SubnetId"]
        for s in ec2.describe_subnets(
            SubnetIds=eks_vpc_config["subnetIds"],
            Filters=[
                {"Name": "tag-key", "Values": ["kubernetes.io/role/internal-elb"]}
            ],
        )["Subnets"]
    ]
    sts = sess.client("sts")
    role_arn = "/".join(
        sts.get_caller_identity()["Arn"]
        .replace(":sts:", ":iam:")
        .replace(":assumed-role/", ":role/")
        .split("/")[:-1]
    )
    lmbd = sess.client("lambda")
    function_config = {
        "FunctionName": f"awsqs-kubernetes-resource-proxy-{cluster_name}",
        "Runtime": "python3.7",
        "Role": role_arn,
        "Handler": "awsqs_kubernetes_resource.utils.proxy_wrap",
        "Timeout": 900,
        "MemorySize": 512,
        "VpcConfig": {
            "SubnetIds": internal_subnets,
            "SecurityGroupIds": eks_vpc_config["securityGroupIds"],
        }
    }
    try:
        no_update = update_function_config(lmbd, function_config)
    except lmbd.exceptions.ResourceNotFoundException:
        no_update = False
        with open("./awsqs_kubernetes_resource/vpc.zip", "rb") as zip_file:
            LOG.debug("Putting lambda function...")
            lmbd.create_function(Code={"ZipFile": zip_file.read()}, **function_config)
            LOG.debug("Done putting lambda function.")
    if not no_update:
        while not update_function_config(lmbd, function_config):
            time.sleep(5)