This message was deleted.
# general
s
This message was deleted.
c
You probably need to set a dependency between the lambda and the s3 bucket
🙏 1
w
Thanks for the reply Yarin, how can I do this?
d
Can we see some code? If you use one resource in another one they should depend automatically
w
@clever-byte-21551 I applied your suggestion and that issue was resolved, thanks again. I have two other issues, one is that if I update the zip file for the lambda, the S3 object gets updated but not the lambda functions. Second is that I often get the no integration error when updating my stack "* Error creating API Gateway Deployment: BadRequestException: No integration defined for method", but when I run a couple of times it would work. I did put all the dependencies. @dazzling-sundown-39670,here are some codes.
Copy code
def deploy_api(self, api, root_apigateway, service, bucket, package_s3_key, package_s3):
        #'api': {'name': 'get_dm_node', 'handler': 'lambda_get_dm_node', 'method': 'POST', 'path':'dmnode/get'}

        api_name =  f'{service["name"]}-{api["name"]}'
        print(f'Deploying {api_name}')
        __api_resource, __last_api_resource = None, None
        path_suffix = ""
        for i, path_part in enumerate(api["path"].split('/')):
            path_suffix = path_suffix + "-" + path_part 
            __api_resource = self.RESOURCES.get(path_suffix, None)
            if i == 0:
                parent_id =  root_apigateway.root_resource_id
            else:
                parent_id =  __last_api_resource.id

            #print(i, path_suffix, __last_api_resource)        
            if __api_resource is None:         
                #Create a new resource.
                __api_resource = apigateway.Resource(path_suffix,
                            rest_api=root_apigateway,
                            parent_id=parent_id,
                            path_part=path_part)

                self.RESOURCES[path_suffix] = __api_resource
            __last_api_resource = __api_resource

        print(f'Resoruce {__api_resource.id} created')
        __vpc_config = lambda_.FunctionVpcConfigArgs(
                                security_group_ids=self.LAMBDA_SECURITY_GROUP_IDS,
                                subnet_ids=self.LAMBDA_SUBNET_IDS)    

        __fn = lambda_.Function(
            api_name,
            s3_bucket=bucket.id,
            s3_key=package_s3_key,
            handler=api["handler"],
            runtime="python3.7",
            role=iam.lambda_role.arn,
            timeout=self.LAMBDA_DEFAULT_TIMEOUT,
            vpc_config=__vpc_config,
            __opts__=ResourceOptions(depends_on=[package_s3])
        )
        print(f'Function {__fn.id} created')

        __proxy_root_met = apigateway.Method(
            api_name,
            rest_api=root_apigateway,
            resource_id=__api_resource.id,
            http_method='ANY',
            authorization='NONE',
            __opts__=ResourceOptions(depends_on=[__api_resource])
        )    
        print(f'Method {__proxy_root_met.id} created')

        __root_int = apigateway.Integration(
            api_name,
            rest_api=root_apigateway,
            resource_id=__proxy_root_met.resource_id,
            http_method=__proxy_root_met.http_method,
            integration_http_method='POST',
            type='AWS_PROXY',
            uri=__fn.invoke_arn,
            __opts__=ResourceOptions(depends_on=[__proxy_root_met, __fn])
        )
        print(f'Integration {__root_int.id} created')

        __dep = apigateway.Deployment(
            f'{api_name}-deploy',
            rest_api=root_apigateway,
            stage_name="mlhere-dev",
            __opts__=ResourceOptions(depends_on=[__root_int])
        )
        print(f'Deployment {__dep.id} created')

        __perm = lambda_.Permission(
            api_name,
            statement_id="AllowAPIGatewayInvoke",
            action="lambda:InvokeFunction",
            function=__fn,
            principal="<http://apigateway.amazonaws.com|apigateway.amazonaws.com>",
            source_arn=__dep.execution_arn.apply(lambda x:f"{x}/*/*")
        )