哇喔,这个已经有一年了,所以你可能找到了其他的方法。但我会将这个放在这里,以便下一个人可以使用。目前 AWS 不是我的最爱 r/unpopularopinion
首先,我通过结合以下三种方法解决了这个问题:
- 首先必须存在一个 Lambda 函数,可以通过使用 AWS CLI、GUI 或者使用
boto3
函数来创建。
lambda_client = boto3.client('lambda')
new_lambda = lambda_client.create_function(
FunctionName='HelloWorld',
Runtime='nodejs12.x',
Role='arn:aws:iam::<user_id>:role/HelloWorld',
Handler='handler.handler',
Code={ 'ZipFile': <zip_file_object_from_s3> },
Description='Hello World Function',
Publish=True,
)
# You can then get at least part of the invocation uri here:
print(new_lambda['FunctionArn'])
- 使用boto3创建一个Rest Api
rest_api = api_client.create_rest_api(
name='GreatApi'
)
print(rest_api)
rest_api_id = rest_api["id"]
root_resource_id = api_client.get_resources(
restApiId=rest_api_id
)['items'][0]['id']
api_resource = api_client.create_resource(
restApiId=rest_api_id,
parentId=root_resource_id,
pathPart='greeting'
)
api_resource_id = api_resource['id']
api_method = api_client.put_method(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
authorizationType='NONE',
requestParameters={
'method.request.querystring.greeter': False
}
)
print(api_method)
put_method_res = api_client.put_method_response(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
statusCode='200'
)
print(put_method_res)
arn_uri="arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:<user_id>:function:HelloWorld/invocations"
put_integration = api_client.put_integration(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
type='AWS',
integrationHttpMethod='POST',
uri=arn_uri
credentials='arn:aws:iam::<user_id>:role/HelloWorldRole',
requestTemplates={
"application/json":"{\"greeter\":\"$input.params('greeter')\"}"
},
)
print(put_integration)
put_integration_response = api_client.put_integration_response(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
statusCode='200',
selectionPattern=''
)
print(put_integration_response)
deployment = api_client.create_deployment(
restApiId=rest_api_id,
stageName='dev',
)
print(deployment)
这需要更多的操作,但我认为如果没有apiGateway,你实际上无法创建资源。
等我有空时,我会构建一个示例无服务器存储库。