This project demonstrates how to create an AWS Lambda function using Go to retrieve dummy weather information and current time. The Lambda function is triggered by an API Gateway endpoint and returns the weather details along with the current timestamp.
The Lambda function performs the following tasks:
- Receives a GET request from an API Gateway endpoint.
- Retrieves dummy weather information (in this example, it is hardcoded as "Sunny").
- Retrieves the current timestamp.
- Constructs a JSON response containing the weather information and timestamp.
- Returns the response to the API Gateway, which is then sent back to the client.
The repository has the following structure:
main.go
: Contains the Go source code for the Lambda function.deployment.zip
: A ZIP file containing the compiled Go binary and its dependencies for deployment to AWS Lambda.README.md
: Provides instructions on how to set up and deploy the Lambda function.
To set up and deploy the Lambda function locally, you need the following prerequisites:
- Go installed and properly configured on your local machine.
- An AWS account.
- The AWS CLI installed and configured on your local machine.
The deployment process involves the following steps:
- Clone the repository.
- Write the Lambda function code.
- Build the Go binary.
- Package the code into a ZIP file.
- Create an AWS Lambda function.
- Upload the code to the Lambda function.
- Set up an API Gateway trigger for the Lambda function.
- Deploy the API.
- Test the Lambda function and API endpoint.
Follow the steps below to set up and deploy the Lambda function.
Clone this repository to your local machine using the following command:
git clone https://github.com/ahmedMHasan/weather-status-aws-lambda-function.git
Create a new file called main.go
and copy the code from the example into it.
Open a terminal and navigate to the project directory. Run the following command to build the Go binary:
go build -o main.exe
Create a ZIP file containing the binary and its dependencies. Zip Files name : deployment.zip
- Go to the AWS Management Console and navigate to the Lambda service.
- Click on "Create function" and choose "Author from scratch."
- Provide a name for your function and select "Go 1.x" as the runtime.
- Under "Permissions," choose "Create a new role with basic Lambda permissions."
- Click on "Create function."
- Scroll down to the "Function code" section.
- Choose "Upload a .zip file" from the "Code entry type" dropdown.
- Click on "Upload" and select the
deployment.zip
file you created earlier. - Set the "Handler" field to the name of your Go binary (e.g.,
main
).
- In the Lambda function configuration page, click on "Add trigger."
- Select "API Gateway" as the trigger type.
- Choose "Create a new API" and provide a name for your API.
- Under "Security," select "Open" or configure authentication as desired.
- Click on "Add" to add the trigger.
- In the API Gateway configuration page, click on "Actions" and select "Deploy API."
- Choose a stage name and click on "Deploy."
- In your Lambda function's configuration page, click on "Test."
- Create a new test event or use the default event template.
- Click on "Test" to execute the Lambda function.
- In the API Gateway configuration page, go to the "Dashboard" tab.
- Copy the "Invoke URL" for your API.
- Use Postman or any other HTTP client to send a GET request to the API endpoint.
- You should receive a response with the dummy weather information and current time.
Congratulations! You have successfully set up and deployed the AWS Lambda function for dummy weather status and current time using Go.
This project is licensed under the MIT License.