forked from sofianhamiti/aws-lambda-r-inference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (86 loc) · 3.23 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from aws_cdk import (
aws_lambda,
aws_iam,
aws_apigatewayv2,
core
)
class InferenceStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
project_name = 'r-inference'
s3_model_uri = '<ADD YOUR S3 MODEL URI HERE>'
# ==================================================
# ================= IAM ROLES ======================
# ==================================================
lambda_role = aws_iam.Role(
scope=self,
id='lambda_role',
assumed_by=aws_iam.ServicePrincipal(service='lambda.amazonaws.com'),
managed_policies=[aws_iam.ManagedPolicy.from_aws_managed_policy_name('AWSLambdaExecute')]
)
api_role = aws_iam.Role(
scope=self,
id='api_role',
assumed_by=aws_iam.ServicePrincipal(service='apigateway.amazonaws.com'),
)
api_role.add_to_policy(aws_iam.PolicyStatement(
effect=aws_iam.Effect.ALLOW,
actions=['lambda:InvokeFunction'],
resources=['*']
))
# ==================================================
# =================== ECR IMAGE ====================
# ==================================================
ecr_image = aws_lambda.DockerImageCode.from_image_asset(
repository_name=project_name,
directory='lambda_image'
)
# ==================================================
# ================ LAMBDA FUNCTION =================
# ==================================================
lambda_function = aws_lambda.DockerImageFunction(
scope=self,
id='lambda',
function_name=project_name,
code=ecr_image,
memory_size=1024,
role=lambda_role,
environment={
'S3_MODEL_URI': s3_model_uri
},
timeout=core.Duration.seconds(30)
)
# ==================================================
# ================== API GATEWAY ===================
# ==================================================
api = aws_apigatewayv2.HttpApi(
scope=self,
id='api_gateway',
api_name=project_name,
cors_preflight={
"allow_headers": ["Authorization"],
"allow_methods": [aws_apigatewayv2.HttpMethod.POST],
"allow_origins": ["*"],
"max_age": core.Duration.days(10)
}
)
integration = aws_apigatewayv2.CfnIntegration(
scope=self,
id='integration',
api_id=api.http_api_id,
credentials_arn=api_role.role_arn,
integration_type='AWS_PROXY',
integration_uri=lambda_function.function_arn,
integration_method='POST',
payload_format_version='2.0'
)
aws_apigatewayv2.CfnRoute(
scope=self,
id='route',
api_id=api.http_api_id,
route_key='POST /',
target=f'integrations/{integration.ref}'
)
app = core.App()
InferenceStack(app, "InferenceStack")
app.synth()