-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from sagittaras/issue/lambda-module
Lambda module
- Loading branch information
Showing
34 changed files
with
1,235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Sagittaras.CDK.Framework.CodeDeploy/Applications/CodeDeployFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Constructs; | ||
using Sagittaras.CDK.Framework.Factory; | ||
|
||
namespace Sagittaras.CDK.Framework.CodeDeploy.Applications; | ||
|
||
/// <summary> | ||
/// Common factory construct for defining the CodeDeploy applications. | ||
/// </summary> | ||
/// <typeparam name="TApplication">Type of resulting application.</typeparam> | ||
/// <typeparam name="TProps">Properties for the application.</typeparam> | ||
public abstract class CodeDeployFactory<TApplication, TProps> : ConstructFactory<TApplication, TProps> | ||
where TApplication : IConstruct | ||
where TProps : class | ||
{ | ||
protected CodeDeployFactory(Construct scope, string applicationName) : base(scope, applicationName) | ||
{ | ||
ApplicationName = Cloudspace.ResourceName(applicationName); | ||
} | ||
|
||
/// <summary> | ||
/// Name of the application prefixed with the Cloud space name. | ||
/// </summary> | ||
protected string ApplicationName { get; } | ||
} |
27 changes: 27 additions & 0 deletions
27
Sagittaras.CDK.Framework.CodeDeploy/Applications/LambdaApplicationFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Amazon.CDK.AWS.CodeDeploy; | ||
using Constructs; | ||
|
||
namespace Sagittaras.CDK.Framework.CodeDeploy.Applications; | ||
|
||
/// <summary> | ||
/// Factory for creating LambdaApplication. | ||
/// </summary> | ||
public class LambdaApplicationFactory : CodeDeployFactory<LambdaApplication, LambdaApplicationProps> | ||
{ | ||
public LambdaApplicationFactory(Construct scope, string applicationName) : base(scope, applicationName) | ||
{ | ||
Props = new LambdaApplicationProps | ||
{ | ||
ApplicationName = ApplicationName | ||
}; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override LambdaApplicationProps Props { get; } | ||
|
||
/// <inheritdoc /> | ||
public override LambdaApplication Construct() | ||
{ | ||
return new LambdaApplication(this, "deploy-app", Props); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
Sagittaras.CDK.Framework.CodeDeploy/Deployments/DeploymentGroupFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Amazon.CDK.AWS.CloudWatch; | ||
using Constructs; | ||
using Sagittaras.CDK.Framework.Extensions; | ||
using Sagittaras.CDK.Framework.Factory; | ||
using Sagittaras.CDK.Framework.Props; | ||
|
||
namespace Sagittaras.CDK.Framework.CodeDeploy.Deployments; | ||
|
||
/// <summary> | ||
/// Base factory for creating a deployment group. | ||
/// </summary> | ||
/// <typeparam name="TDeploymentGroup"></typeparam> | ||
/// <typeparam name="TProps"></typeparam> | ||
public abstract class DeploymentGroupFactory<TDeploymentGroup, TProps> : ConstructFactory<TDeploymentGroup, TProps> | ||
where TDeploymentGroup : IConstruct | ||
where TProps : class | ||
{ | ||
protected DeploymentGroupFactory(Construct scope, string groupName) : base(scope, groupName) | ||
{ | ||
CommonProps = new DeploymentGroupProps | ||
{ | ||
DeploymentGroupName = groupName | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Common props for deployment groups. | ||
/// </summary> | ||
private DeploymentGroupProps CommonProps { get; } | ||
|
||
/// <inheritdoc /> | ||
public override TDeploymentGroup Construct() | ||
{ | ||
PropsMapper.Map(CommonProps, Props); | ||
return ConstructDeploymentGroup(); | ||
} | ||
|
||
/// <summary> | ||
/// Replaces original <see cref="Construct" /> to allow mapping of common properties to the deployment group. | ||
/// </summary> | ||
/// <returns></returns> | ||
protected abstract TDeploymentGroup ConstructDeploymentGroup(); | ||
|
||
/// <summary> | ||
/// Adds alarms to the deployment group that should watch the deployment. | ||
/// </summary> | ||
/// <param name="alarms"></param> | ||
/// <returns></returns> | ||
public DeploymentGroupFactory<TDeploymentGroup, TProps> AddAlarms(params IAlarm[] alarms) | ||
{ | ||
CommonProps.AddAlarms(alarms); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Adds alarms to the deployment group that should watch the deployment. | ||
/// </summary> | ||
/// <remarks> | ||
/// Finds the alarms by their names. | ||
/// </remarks> | ||
/// <param name="alarmNames"></param> | ||
/// <returns></returns> | ||
public DeploymentGroupFactory<TDeploymentGroup, TProps> AddAlarmsByLookup(params string[] alarmNames) | ||
{ | ||
IAlarm[] alarms = alarmNames.Select(x => Alarm.FromAlarmName(this, $"alarm-{x.ToResourceId()}", x)) | ||
.ToArray(); | ||
AddAlarms(alarms); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configures whether to ignore alarms during deployment. | ||
/// </summary> | ||
/// <param name="ignore"></param> | ||
/// <returns></returns> | ||
public DeploymentGroupFactory<TDeploymentGroup, TProps> IgnoreAlarms(bool ignore) | ||
{ | ||
CommonProps.IgnorePollAlarmsFailure = ignore; | ||
|
||
return this; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Sagittaras.CDK.Framework.CodeDeploy/Deployments/DeploymentGroupProps.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Amazon.CDK.AWS.CloudWatch; | ||
using Amazon.CDK.AWS.CodeDeploy; | ||
|
||
namespace Sagittaras.CDK.Framework.CodeDeploy.Deployments; | ||
|
||
/// <summary> | ||
/// Custom class to group common properties of the deployment groups of different types, | ||
/// to overcome missing base class for deployment groups. | ||
/// </summary> | ||
public class DeploymentGroupProps | ||
{ | ||
private readonly List<IAlarm> _alarms = new(); | ||
|
||
public IAlarm[]? Alarms | ||
{ | ||
get => _alarms.Any() ? _alarms.ToArray() : null; | ||
set | ||
{ | ||
if (value is null) return; | ||
|
||
_alarms.AddRange(value); | ||
} | ||
} | ||
|
||
public AutoRollbackConfig AutoRollback { get; set; } = new() | ||
{ | ||
FailedDeployment = true, | ||
StoppedDeployment = true | ||
}; | ||
|
||
public string? DeploymentGroupName { get; set; } | ||
public bool? IgnorePollAlarmsFailure { get; set; } | ||
|
||
public void AddAlarms(params IAlarm[] alarms) | ||
{ | ||
_alarms.AddRange(alarms); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
Sagittaras.CDK.Framework.CodeDeploy/Deployments/LambdaDeploymentGroupFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Amazon.CDK.AWS.CodeDeploy; | ||
using Amazon.CDK.AWS.Lambda; | ||
using Constructs; | ||
|
||
namespace Sagittaras.CDK.Framework.CodeDeploy.Deployments; | ||
|
||
/// <summary> | ||
/// Factory for creating a deployment group for Lambda function. | ||
/// </summary> | ||
public class LambdaDeploymentGroupFactory : DeploymentGroupFactory<LambdaDeploymentGroup, LambdaDeploymentGroupProps> | ||
{ | ||
public LambdaDeploymentGroupFactory(Construct scope, string groupName, Function function) : base(scope, groupName) | ||
{ | ||
Props = new LambdaDeploymentGroupProps | ||
{ | ||
Alias = function.AddAlias("Live", new AliasOptions | ||
{ | ||
Description = "Alias for the blue/green deployment." | ||
}) | ||
}; | ||
} | ||
|
||
public LambdaDeploymentGroupFactory(Construct scope, string groupName, Alias alias) : base(scope, groupName) | ||
{ | ||
Props = new LambdaDeploymentGroupProps | ||
{ | ||
Alias = alias | ||
}; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override LambdaDeploymentGroupProps Props { get; } | ||
|
||
/// <inheritdoc /> | ||
protected override LambdaDeploymentGroup ConstructDeploymentGroup() | ||
{ | ||
return new LambdaDeploymentGroup(this, "deployment-group", Props); | ||
} | ||
|
||
/// <summary> | ||
/// Makes the deployment group part of the application. | ||
/// </summary> | ||
/// <param name="application"></param> | ||
/// <returns></returns> | ||
public LambdaDeploymentGroupFactory PartOf(ILambdaApplication application) | ||
{ | ||
Props.Application = application; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Makes the deployment group part of the application. | ||
/// </summary> | ||
/// <remarks> | ||
/// Find the application by its name. | ||
/// </remarks> | ||
/// <param name="applicationName"></param> | ||
/// <returns></returns> | ||
public LambdaDeploymentGroupFactory PartOf(string applicationName) | ||
{ | ||
Props.Application = LambdaApplication.FromLambdaApplicationName(this, "application", applicationName); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the deployment strategy for the group. | ||
/// </summary> | ||
/// <param name="config"></param> | ||
/// <returns></returns> | ||
public LambdaDeploymentGroupFactory WithDeploymentConfig(ILambdaDeploymentConfig config) | ||
{ | ||
Props.DeploymentConfig = config; | ||
return this; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Sagittaras.CDK.Framework.CodeDeploy/Sagittaras.CDK.Framework.CodeDeploy.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Version>1.0.0-preview-01</Version> | ||
<Title>CodeDeploy CDK Framework</Title> | ||
<Description>Framework for easier building fo CodeDeploy with CDK.</Description> | ||
<PackageProjectUrl>https://github.com/sagittaras/cdk.framework</PackageProjectUrl> | ||
<PackageLicenseUrl>https://github.com/sagittaras/cdk.framework/blob/main/LICENSE</PackageLicenseUrl> | ||
<RepositoryUrl>https://github.com/sagittaras/cdk.framework</RepositoryUrl> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Sagittaras.CDK.Framework\Sagittaras.CDK.Framework.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
40 changes: 40 additions & 0 deletions
40
Sagittaras.CDK.Framework.Lambda/DockerImageLambdaFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Amazon.CDK.AWS.ECR; | ||
using Amazon.CDK.AWS.Lambda; | ||
using Constructs; | ||
|
||
namespace Sagittaras.CDK.Framework.Lambda; | ||
|
||
/// <summary> | ||
/// Factory creating a Lambda function with usage of Docker Image. | ||
/// </summary> | ||
public class DockerImageLambdaFactory : LambdaFactory<DockerImageFunction, DockerImageFunctionProps> | ||
{ | ||
public DockerImageLambdaFactory(Construct scope, string functionName) : base(scope, functionName) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override DockerImageFunctionProps Props { get; } = new(); | ||
|
||
/// <inheritdoc /> | ||
public override DockerImageFunction Construct() | ||
{ | ||
MapCommonProperties(Props); | ||
return new DockerImageFunction(this, "function", Props); | ||
} | ||
|
||
/// <summary> | ||
/// Configure the usage of docker image from ECR, based on tag or digest. | ||
/// </summary> | ||
/// <param name="repository"></param> | ||
/// <param name="tagOrDigest"></param> | ||
/// <returns></returns> | ||
public DockerImageLambdaFactory FromEcr(string repository, string tagOrDigest) | ||
{ | ||
Props.Code = DockerImageCode.FromEcr(Repository.FromRepositoryName(this, "image-source", repository), new EcrImageCodeProps | ||
{ | ||
TagOrDigest = tagOrDigest | ||
}); | ||
return this; | ||
} | ||
} |
Oops, something went wrong.