Skip to content

Commit

Permalink
Modify input JSON data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
yang0352 committed Apr 10, 2025
1 parent 02c4151 commit 7f1964e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 54 deletions.
65 changes: 43 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
This repository contains source code and supporting files for a serverless application that you can deploy with the SAM CLI.
The application uses a Lambda function to process JSON input data and create a new GitHub repo for **Census EKS CI/CD pipeline**.


## Getting Started

First of all, you need access to an AWS account with adequate permission to which the resources will be deployed.
Expand Down Expand Up @@ -34,7 +33,6 @@ You may need to submit a support ticket to request the installation of these too

### Installing


- Clone this repository:

```sh
Expand Down Expand Up @@ -85,6 +83,26 @@ You may need to submit a support ticket to request the installation of these too

- Test:

The input `JSON` payload is in the following format:

```json
{
"project_name": "string",
"eks_settings": {
"attrs": {
"attribute1": "value1",
"attribute2": "value2",
...
},
"tags" : {
"key1": "value1",
"key2": "value2",
...
}
}
}
```

Get the `API Key`:

```sh
Expand All @@ -94,26 +112,29 @@ You may need to submit a support ticket to request the installation of these too
```sh
curl -X POST -H "X-API-Key: {API Key}" https://{API Gateway endpoint URL} -d '
{
"attrs": {
"account_name": "lab-dev-ew",
"aws_region": "us-gov-east-1",
"cluster_mailing_list": "matthew.c.morgan@census.gov",
"cluster_name": "csvd-platform-lab-mcm",
"eks_instance_disk_size": 100,
"eks_ng_desired_size": 2,
"eks_ng_max_size": 10,
"eks_ng_min_size": 2,
"environment": "development",
"environment_abbr": "dev",
"organization": "census:ocio:csvd",
"finops_project_name": "csvd_platformbaseline",
"finops_project_number": "fs0000000078",
"finops_project_role": "csvd_platformbaseline_app",
"vpc_domain_name": "dev.lab.csp2.census.gov",
"vpc_name": "vpc3-lab-dev"
},
"tags" : {
"slim:schedule": "8:00-17:00"
"project_name": "eks-automation-lambda-test",
"eks_settings": {
"attrs": {
"account_name": "lab-dev-ew",
"aws_region": "us-gov-east-1",
"cluster_mailing_list": "someone@census.gov",
"cluster_name": "csvd-platform-lab-mcm",
"eks_instance_disk_size": 100,
"eks_ng_desired_size": 2,
"eks_ng_max_size": 10,
"eks_ng_min_size": 2,
"environment": "development",
"environment_abbr": "dev",
"organization": "census:ocio:csvd",
"finops_project_name": "csvd_platformbaseline",
"finops_project_number": "fs0000000078",
"finops_project_role": "csvd_platformbaseline_app",
"vpc_domain_name": "dev.lab.csp2.census.gov",
"vpc_name": "vpc3-lab-dev"
},
"tags" : {
"slim:schedule": "8:00-17:00"
}
}
}
'
Expand Down
30 changes: 18 additions & 12 deletions eks_automation/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@
SECRET_NAME = "/dev/eks_automation_github_token"

ORIG_REPO_NAME = "platform-tg-infra"
NEW_REPO_NAME = "eks-automation-lambda-test1"

TEMPLATE_FILE_NAME = "eks.hcl.j2"
HCL_FILE_NAME = "eks.hcl"

# DATA_FILE_NAME = "data.json"

# Initialize the logger
logger = logging.getLogger()
logger.setLevel("INFO")
Expand All @@ -57,12 +54,21 @@ def lambda_handler(event, context):

# For test, load input data from a local file.
# input_data = ""
# with open(DATA_FILE_NAME, "r") as file:
# with open("data.json", "r") as file:
# input_data = json.load(file)

input_data = json.loads(event["body"])

project_name = input_data["project_name"]
eks_settings = input_data["eks_settings"]
if not project_name:
return {
"statusCode": 400,
"body": json.dumps({"error": "Missing project name"}),
}

try:
input_data = json.loads(event["body"])
rendered = operate_github(NEW_REPO_NAME, input_data, HCL_FILE_NAME)
rendered = operate_github(project_name, eks_settings, HCL_FILE_NAME)
except Exception as e: # pylint: disable=broad-exception-caught
return {"statusCode": 400, "body": json.dumps({"error": str(e)})}

Expand All @@ -73,13 +79,13 @@ def lambda_handler(event, context):
}


def operate_github(new_repo_name, json_data, output_hcl):
def operate_github(new_repo_name, eks_settings, output_hcl):
"""Clone a GitHub repo, add an EKS parameter file rendered
from a template and the input JSON dta, and push to a new repo.
Args:
new_repo_name (str): Name of the new GitHub repo.
json_data (json): Input JSON data with all the EKS parameter values.
eks_settings (json): Input JSON data with all the EKS parameter values.
output_hcl (str): Name of the EKS parameter file in HCL format.
Returns:
Expand Down Expand Up @@ -123,7 +129,7 @@ def operate_github(new_repo_name, json_data, output_hcl):
current_branch.rename("main", force=True)

# Render the j2 template using the input data.
rendered = render_j2_template(json_data, TEMPLATE_FILE_NAME)
rendered = render_j2_template(eks_settings, TEMPLATE_FILE_NAME)
# Write the renderd data to a file in the local staging repository root directory
with open(f"/tmp/{new_repo_name}/{output_hcl}", "w") as file:
file.write(rendered)
Expand Down Expand Up @@ -168,11 +174,11 @@ def get_repo(org, repo_name, create=False):
return repo


def render_j2_template(json_data, j2_template, j2_template_dir="templates/"):
def render_j2_template(eks_settings, j2_template, j2_template_dir="templates/"):
"""Render the j2 template with the input JSON data
Args:
json_data (json): input data in JSON format.
eks_settings (json): input data in JSON format.
j2_template (j2): Name of the template file to generate the output.
j2_template_dir (str, optional): The directory where the templates are stored. Defaults to "templates/".
Expand All @@ -184,7 +190,7 @@ def render_j2_template(json_data, j2_template, j2_template_dir="templates/"):
jinja_env = Environment(loader=FileSystemLoader(j2_template_dir), trim_blocks=True)
template = jinja_env.get_template(j2_template)

return template.render(data=json_data)
return template.render(data=eks_settings)


def github_org(base_url, org_name, token):
Expand Down
43 changes: 23 additions & 20 deletions eks_automation/data.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
{
"attrs": {
"account_name": "lab-dev-ew",
"aws_region": "us-gov-east-1",
"cluster_mailing_list": "matthew.c.morgan@census.gov",
"cluster_name": "csvd-platform-lab-mcm",
"eks_instance_disk_size": 100,
"eks_ng_desired_size": 2,
"eks_ng_max_size": 10,
"eks_ng_min_size": 2,
"environment": "development",
"environment_abbr": "dev",
"organization": "census:ocio:csvd",
"finops_project_name": "csvd_platformbaseline",
"finops_project_number": "fs0000000078",
"finops_project_role": "csvd_platformbaseline_app",
"vpc_domain_name": "dev.lab.csp2.census.gov",
"vpc_name": "vpc3-lab-dev"
},
"tags" : {
"slim:schedule": "8:00-17:00"
"project_name": "eks-automation-lambda-test1",
"eks_settings": {
"attrs": {
"account_name": "lab-dev-ew",
"aws_region": "us-gov-east-1",
"cluster_mailing_list": "matthew.c.morgan@census.gov",
"cluster_name": "csvd-platform-lab-mcm",
"eks_instance_disk_size": 100,
"eks_ng_desired_size": 2,
"eks_ng_max_size": 10,
"eks_ng_min_size": 2,
"environment": "development",
"environment_abbr": "dev",
"organization": "census:ocio:csvd",
"finops_project_name": "csvd_platformbaseline",
"finops_project_number": "fs0000000078",
"finops_project_role": "csvd_platformbaseline_app",
"vpc_domain_name": "dev.lab.csp2.census.gov",
"vpc_name": "vpc3-lab-dev"
},
"tags" : {
"slim:schedule": "8:00-17:00"
}
}
}

0 comments on commit 7f1964e

Please sign in to comment.