diff --git a/.github/workflows/cd-lambda.yml b/.github/workflows/cd-lambda.yml new file mode 100644 index 0000000..0fb4bd7 --- /dev/null +++ b/.github/workflows/cd-lambda.yml @@ -0,0 +1,61 @@ +name: Let's Deploy Cron Lambda + +on: + workflow_run: + workflows: ["Let's Go CI"] + types: + - completed + branches: [main] + +jobs: + deploy-lambda: + if: | + github.event.workflow_run.conclusion == 'success' && + github.event.workflow_run.head_branch == 'main' + runs-on: ubuntu-latest + + steps: + - name: Checkout Source + uses: actions/checkout@v4 + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v42 + + - name: Check if relevant files changed + id: check-changes + run: | + RELEVANT=false + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do + if [[ $file == cmd/cron/* ]]; then + RELEVANT=true + break + fi + done + if [ "$RELEVANT" = "false" ]; then + echo "No relevant files changed, skipping deployment" + exit 78 # Special exit code that GitHub Actions recognizes as neutral + fi + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.22.x' + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_KEY }} + aws-secret-access-key: ${{ secrets.AWS_SECRET }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Build Lambda Function + run: | + GOOS=linux GOARCH=amd64 go build -o bootstrap cmd/cron/main.go + zip go-lambda.zip bootstrap + + - name: Deploy to Lambda + run: | + aws lambda update-function-code \ + --function-name ${{ secrets.AWS_LAMBDA_FUNCTION_NAME }} \ + --zip-file fileb://go-lambda.zip \ No newline at end of file diff --git a/cmd/cron/main.go b/cmd/cron/main.go new file mode 100644 index 0000000..8e366e5 --- /dev/null +++ b/cmd/cron/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "database/sql" + "fmt" + "os" + "time" + + "github.com/aws/aws-lambda-go/lambda" + adapter "github.com/eyo-chen/expense-tracker-go/internal/adapter" + "github.com/eyo-chen/expense-tracker-go/internal/usecase/monthlytrans" + "github.com/eyo-chen/expense-tracker-go/pkg/logger" + _ "github.com/go-sql-driver/mysql" +) + +func main() { + lambda.Start(handleRequest) +} + +func handleRequest(ctx context.Context) error { + logger.Register() + + logger.Info("Connecting to database...") + mysqlDB, err := newMysqlDB() + if err != nil { + logger.Error("Unable to connect to mysql database", "error", err) + return err + } + defer mysqlDB.Close() + + // Setup adapter and usecase + adapter := adapter.New(mysqlDB, nil, nil, nil, "") + monthlyTransUC := monthlytrans.New(adapter.MonthlyTrans, adapter.Transaction) + + // Execute monthly transaction aggregation for previous month + previousMonth := time.Now().AddDate(0, -1, 0) + if err := monthlyTransUC.Create(ctx, previousMonth); err != nil { + logger.Error("Failed to create monthly transaction aggregation", "error", err) + return err + } + + logger.Info("Successfully created monthly transaction aggregation", "month", previousMonth.Format(time.DateOnly)) + return nil +} + +func newMysqlDB() (*sql.DB, error) { + config := map[string]string{ + "host": os.Getenv("DB_HOST"), + "port": os.Getenv("DB_PORT"), + "name": os.Getenv("DB_NAME"), + "user": os.Getenv("DB_USER"), + "password": os.Getenv("DB_PASSWORD"), + } + + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", config["user"], config["password"], config["host"], config["port"], config["name"]) + db, err := sql.Open("mysql", dsn) + if err != nil { + return nil, err + } + + if err = db.Ping(); err != nil { + return nil, err + } + + return db, nil +} diff --git a/go.mod b/go.mod index c41104b..84358fe 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect + github.com/aws/aws-lambda-go v1.47.0 // indirect github.com/aws/aws-sdk-go-v2 v1.31.0 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.5 // indirect github.com/aws/aws-sdk-go-v2/config v1.27.36 // indirect diff --git a/go.sum b/go.sum index 8ea1ab8..8e7fd22 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= +github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI= +github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A= github.com/aws/aws-sdk-go-v2 v1.30.5 h1:mWSRTwQAb0aLE17dSzztCVJWI9+cRMgqebndjwDyK0g= github.com/aws/aws-sdk-go-v2 v1.30.5/go.mod h1:CT+ZPWXbYrci8chcARI3OmI/qgd+f6WtuLOoaIA8PR0= github.com/aws/aws-sdk-go-v2 v1.31.0 h1:3V05LbxTSItI5kUqNwhJrrrY1BAXxXt0sN0l72QmG5U=