Skip to content

Commit 89de6ee

Browse files
Merge pull request #3 from Emmy-github-webdev/staging
Staging
2 parents 80a95f8 + 46b9f59 commit 89de6ee

3 files changed

Lines changed: 81 additions & 43 deletions

File tree

README.md

Lines changed: 81 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
The goal of this project is to build, configure, and automate the deployment of a simple serverless application on AWS. Created a health check endpoint that logs requests and stores them in a database, with a CI/CD pipeline to manage deployments for both staging and production environments, fully provisioned via Terraform and deployed automatically using GitHub Actions.
44

5+
## Prerequisites
6+
- An AWS account and IAM user with rights to create IAM, Lambda, API Gateway, DynamoDB, CloudWatch.
7+
- Local toolchain:
8+
- Terraform v1.2+
9+
- GitHub repo secrets (Repository > Settings > Secrets):
10+
- `AWS_ACCESS_KEY_ID`
11+
- `AWS_SECRET_ACCESS_KEY`
12+
- `AWS_REGION` (e.g. `us-east-1`)
13+
514
## Architectural desig
615

716
### Core Components
@@ -14,6 +23,8 @@ The goal of this project is to build, configure, and automate the deployment of
1423

1524
Each environment (staging, prod) is isolated by naming convention and Terraform variables.
1625

26+
[Live Demo Link](https://www.loom.com/share/3b1a44b435724293b7be8953ed51ae27)
27+
1728
### Runtime Request Flow
1829

1930
1. _Client_: sends a GET or POST request to:
@@ -60,6 +71,7 @@ https://<api-id>.execute-api.<region>.amazonaws.com/health
6071
| Terraform vars | `staging.tfvars` | `prod.tfvars` |
6172
| Lambda | `staging-health-check-function` | `prod-health-check-function` |
6273
| DynamoDB | `staging-requests-db` | `prod-requests-db` |
74+
| S3 Bucket | `serverless-health-check-api/staging/tfstate` | `serverless-health-check-api/prod/tfstate`|
6375
| API Gateway | `staging-health-check-api` | `prod-health-check-api` |
6476
| Approval | None | Required |
6577

@@ -80,50 +92,76 @@ Each Lambda function has one dedicated IAM role with:
8092
- No credentials committed to repository
8193

8294

83-
84-
85-
86-
87-
88-
89-
create hello lambda funtion using Python
90-
95+
![](/svrhecheck.drawio.png)
96+
97+
## Setup
98+
99+
_Repository Structure_
100+
101+
```text
102+
.
103+
├── .github
104+
│ └── workflows
105+
│ ├── deploy.yml
106+
│ └── destroy.yml
107+
├── lambda
108+
│ └── lambda_function.py
109+
├── terraform
110+
│ ├── provider.tf
111+
│ ├── variables.tf
112+
│ ├── main.tf
113+
│ ├── iam.tf
114+
│ ├── lambda.tf
115+
│ ├── apigw.tf
116+
│ ├── outputs.tf
117+
│ ├── staging.tfvars
118+
│ ├── prod.tfvars
119+
│ ├── backend-prod.tfvars
120+
│ └── backend-staging.tfvars
121+
└── README.md
122+
123+
- Create the lambda function, run the python funtion locally using VS Code Run Button, import boto3, logging,json, os, uuid.
124+
- Resource names follows _env-resource-name_
125+
126+
## How to trigger deployments
127+
128+
_Manual for test_
129+
- terraform init -backend-config=backend-staging.tfvars for staging environment
130+
- terraform init -backend-config=backend-prod.tfvars for prod environment
131+
- terraform plan -backend-config=backend-staging.tfvars for staging environment
132+
- terraform plan -backend-config=backend-prod.tfvars for prod environment
133+
- terraform apply -var-file="staging.tfvars"
134+
- terraform apply -var-file="prod.tfvars"
135+
- terraform destroy -backend-config=backend-staging.tfvars for staging environment
136+
- terraform destroy -backend-config=backend-prod.tfvars for prod environment
137+
138+
_GitHub Action CICD_
139+
### Staging
140+
1. Push to branch `staging` (or open a PR merging into `staging` and merge).
141+
2. GitHub Action will automatically:
142+
- `terraform init`
143+
- `terraform plan -var-file="staging.tfvars"`
144+
- `terraform apply` (auto-approved)
145+
146+
### Production
147+
1. Push to branch `main` (or merge PR into `main`).
148+
2. Workflow will run and wait for **manual approval** in the `production` environment (configure environment reviewers in Settings).
149+
3. Approve in GitHub UI → workflow will `apply` the `prod.tfvars` plan.
150+
151+
### Trigger Destroy CICD pipeline
152+
Manually trigger the terraform destroy workflow in GitHub UI -> actions -> destroy.yaml
153+
154+
## Testing the /health endpoint
155+
156+
After deployment, Terraform outputs the API endpoint. You can fetch it:
157+
- From the Actions run logs (last step runs `terraform output -json`)
158+
159+
Example curl (replace `<API_ENDPOINT>` with the `api_endpoint` output):
91160
```
92-
# Hello lambda function
93-
def lambda_handler(event, context):
94-
name = event.get("name", "World")
95-
message = f"Hello, {name}!"
96-
97-
return {
98-
"statusCode": 200,
99-
"body": message
100-
}
101-
102-
# Run the funtion locally for testing
103-
if __name__ == "__main__":
104-
test_event = {"name": "Emmanuel Ogah"}
105-
result = lambda_handler(test_event, None)
106-
print(result)
107-
```
108-
109-
Run the python funtion locally using VS Code Run Button
110-
- Click the “Run Python File” button in the top right corner.
161+
# GET
162+
curl "https://nrbefv9bcj.execute-api.us-east-1.amazonaws.com/health"
111163

112-
```
113164
# Output
165+
{"status": "healthy", "message": "Request processed and saved.", "id": "0fb4fa88-64a1-440d-84b1-5a6d4354364b"}
114166

115-
{'statusCode': 200, 'body': 'Hello, Emmanuel Ogah!'}"
116-
```
117-
118-
- Create the terraform folder structure
119-
120-
- Deploy with: terraform init then terraform apply -var-file="staging.tfvars" (or prod.tfvars)
121-
122-
123-
endpoint - https://nrbefv9bcj.execute-api.us-east-1.amazonaws.com/health
124-
125-
126-
127-
terraform init -backend-config=backend-staging.tfvars for staging environment
128-
129-
terraform init -backend-config=backend-prod.tfvars for prod environment
167+
```

svrhecheck.drawio.png

37.9 KB
Loading
-745 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)