Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌐 End-to-End CI/CD AWS Lambda Deployment Pipeline (with IaC) #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Deploy to AWS Lambda

on:
push:
branches:
- 'main'
- 'master'

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

- name: Install dependencies
run: dotnet restore ./ChauMundo/ChauMundo.csproj

- name: Build project
run: dotnet publish ./ChauMundo/ChauMundo.csproj -c Release -o ./publish

- name: Zip Lambda package
run: |
cd publish
zip -r ../my-lambda.zip .

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Upload to S3
run: aws s3 cp my-lambda.zip s3://${{ secrets.S3_BUCKET_PREFIX }}-dotnetcodebucket/my-lambda.zip

- name: Update Lambda function code
run: |
aws lambda update-function-code --function-name aws-lambda-dotnet-code --s3-bucket ${{ secrets.S3_BUCKET_PREFIX }}-dotnetcodebucket --s3-key my-lambda.zip
6 changes: 0 additions & 6 deletions ChauMundo/App.config

This file was deleted.

7 changes: 6 additions & 1 deletion ChauMundo/ChauMundo.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -9,6 +9,11 @@

<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="9.0.0" />
<PackageReference Include="Amazon.Lambda.AspNetCoreServer.Hosting" Version="1.7.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.3" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions ChauMundo/ChauMundo.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ChauMundo_HostAddress = http://localhost:5133

GET {{ChauMundo_HostAddress}}/hello/
Accept: application/json

###
21 changes: 21 additions & 0 deletions ChauMundo/LambdaEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.AspNetCoreServer;
using Amazon.Lambda.AspNetCoreServer.Internal;
using Amazon.Lambda.Core;

namespace ChauMundo
{
public class LambdaEntryPoint : APIGatewayHttpApiV2ProxyFunction
{
protected override void Init(IWebHostBuilder builder)
{
builder
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
})
.UseStartup<Startup>();
}
}
}
25 changes: 15 additions & 10 deletions ChauMundo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
using System.Configuration;
using ChauMundo.Printers;
using ChauMundo.Printers;

namespace ChauMundo
{
internal class Program
{
static string? Message = null;
static void Main(string[] args)
//static string? Message = null;
public static void Main(string[] args)
{
Message = ConfigurationManager.AppSettings[nameof(Message)];
PrintMessage(new DebugPrinter(), Message ?? "Default message");
Console.ReadLine();
//Message = ConfigurationManager.AppSettings[nameof(Message)];
//PrintMessage(new DebugPrinter(), Message ?? "Default message");

// Start the web application
CreateHostBuilder(args).Build().Run();
}
static void PrintMessage(IPrinter Printer,
string Message)

public static void PrintMessage(IPrinter Printer, string Message)
{
Printer.Print(Message);
}

private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
}
}
41 changes: 41 additions & 0 deletions ChauMundo/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:23440",
"sslPort": 44341
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5133",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7271;http://localhost:5133",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
44 changes: 44 additions & 0 deletions ChauMundo/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using ChauMundo.Printers;

namespace ChauMundo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/hello", () =>
{
string? message = Configuration["Message"];
Program.PrintMessage(new DebugPrinter(), message ?? "Default message");
return Results.Ok("Message printed to debug output");
});
});
}
}
}
9 changes: 9 additions & 0 deletions ChauMundo/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Message": "Hello from DEV appsettings!",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
10 changes: 10 additions & 0 deletions ChauMundo/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Message": "Hello from PROD appsettings!",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Loading