-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaFunctionConstruct.cs
More file actions
159 lines (142 loc) · 5.63 KB
/
Copy pathLambdaFunctionConstruct.cs
File metadata and controls
159 lines (142 loc) · 5.63 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using Amazon.CDK;
using Amazon.CDK.AWS.IAM;
using Amazon.CDK.AWS.Lambda;
using Amazon.CDK.AWS.Logs;
using Constructs;
using LayeredCraft.Cdk.Constructs.Extensions;
using LayeredCraft.Cdk.Constructs.Models;
namespace LayeredCraft.Cdk.Constructs;
public class LambdaFunctionConstruct : Construct
{
/// <summary>
/// The domain of the function URL if GenerateUrl is enabled, null otherwise.
/// </summary>
public readonly string? LiveAliasFunctionUrlDomain;
/// <summary>
/// The underlying Lambda function for advanced configuration.
/// </summary>
public readonly Function LambdaFunction;
public LambdaFunctionConstruct(Construct scope, string id, ILambdaFunctionConstructProps props) : base(scope, id)
{
var region = Stack.Of(this).Region;
var account = Stack.Of(this).Account;
var policy = new Policy(this, $"{id}-policy", new PolicyProps
{
PolicyName = props.PolicyName,
Statements =
[
new PolicyStatement(new PolicyStatementProps
{
Actions =
[
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:TagResource"
],
Resources = [$"arn:aws:logs:{region}:{account}:log-group:/aws/lambda/{props.FunctionName}-{props.FunctionSuffix}*:*"],
Effect = Effect.ALLOW
}),
new PolicyStatement(new PolicyStatementProps
{
Actions =
[
"logs:PutLogEvents"
],
Resources =
[$"arn:aws:logs:{region}:{account}:log-group:/aws/lambda/{props.FunctionName}-{props.FunctionSuffix}*:*:*"],
Effect = Effect.ALLOW
})
]
});
policy.AddStatements(props.PolicyStatements);
var role = new Role(this, $"{id}-role", new RoleProps
{
AssumedBy = new ServicePrincipal("lambda.amazonaws.com"),
RoleName = props.RoleName
});
role.AttachInlinePolicy(policy);
var logGroup = new LogGroup(this, $"{id}-log-group", new LogGroupProps
{
LogGroupName = $"/aws/lambda/{props.FunctionName}-{props.FunctionSuffix}",
Retention = RetentionDays.TWO_WEEKS,
RemovalPolicy = RemovalPolicy.DESTROY
});
LambdaFunction = new Function(this, $"{id}-function", new FunctionProps
{
FunctionName = $"{props.FunctionName}-{props.FunctionSuffix}",
Runtime = Runtime.PROVIDED_AL2023,
Handler = "bootstrap",
Code = Code.FromAsset(props.AssetPath),
Role = role,
MemorySize = props.MemorySize,
Timeout = Duration.Seconds(props.TimeoutInSeconds),
Environment = props.EnvironmentVariables,
LogGroup = logGroup,
Tracing = props.IncludeOtelLayer ? Tracing.ACTIVE : Tracing.DISABLED,
CurrentVersionOptions = new VersionOptions
{
RemovalPolicy = RemovalPolicy.RETAIN
}
});
if (props.IncludeOtelLayer)
{
LambdaFunction.AddLayers(LayerVersion.FromLayerVersionArn(this, "OTELLambdaLayer",
$"arn:aws:lambda:{region}:901920570463:layer:aws-otel-collector-{props.Architecture}-ver-{props.OtelLayerVersion}:1"));
}
// ✅ Create a new version on every deployment
var currentVersion = LambdaFunction.CurrentVersion;
if (props.EnableSnapStart)
{
var cfnFunction = (CfnFunction)LambdaFunction.Node.DefaultChild!;
cfnFunction.AddPropertyOverride("SnapStart", new Dictionary<string, object>
{
["ApplyOn"] = "PublishedVersions"
});
}
var alias = new Alias(this, $"{id}-alias", new AliasProps
{
AliasName = "live",
Version = currentVersion,
});
AddPermissionsToAllTargets(
baseId: $"{id}-permission",
function: LambdaFunction,
version: LambdaFunction.CurrentVersion,
alias: alias,
permissions: props.Permissions
);
if (props.GenerateUrl)
{
var functionUrl = alias.AddFunctionUrl(new FunctionUrlProps
{
AuthType = FunctionUrlAuthType.NONE
});
LiveAliasFunctionUrlDomain = Fn.Select(2, Fn.Split("/", functionUrl.Url));
_ = new CfnOutput(this, $"{id}-url-output", new CfnOutputProps
{
ExportName = Stack.Of(this).CreateExportName(id, "url-output"),
Value = functionUrl.Url
});
}
else
{
LiveAliasFunctionUrlDomain = null;
}
}
private void AddPermissionsToAllTargets(string baseId, IFunction function, IVersion version, IAlias? alias, List<LambdaPermission> permissions)
{
foreach (var (perm, index) in permissions.Select((p, i) => (p, i)))
{
var permission = new Permission
{
Principal = new ServicePrincipal(perm.Principal),
Action = perm.Action,
EventSourceToken = perm.EventSourceToken,
SourceArn = perm.SourceArn
};
function.AddPermission($"{baseId}-fn-{index}", permission);
version.AddPermission($"{baseId}-ver-{index}", permission);
alias?.AddPermission($"{baseId}-alias-{index}", permission);
}
}
}