Ends in
00
days
00
hrs
00
mins
00
secs
ENROLL NOW

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Build Your Own Slack Commands with Lambda Function URL

Home » AWS » Build Your Own Slack Commands with Lambda Function URL

Build Your Own Slack Commands with Lambda Function URL

Last updated on April 27, 2023

What are Lambda Function URLs?

In the past, calling a Lambda function over HTTP required a lot of setup and configuration with API Gateway. The process of setting up API resources and methods, and linking them to the appropriate Lambda function, can be overwhelming, especially for developers who are new to it.

Build Your Own Slack commands with Lambda function URL DVA-C02 examtopics

With the function URL feature of the AWS Lambda service, you can create a secure HTTPS endpoint dedicated to a Lambda function. This means that instead of using an intermediary service (like Amazon API Gateway), you can directly invoke your function by simply sending an HTTP request to its unique URL. It’s an easy and convenient way to run a Lambda function, especially if it’s only meant to be used by a specific service or client.

Function URL endpoints have the following format:

https://<url-id>.lambda-url.<region>.on.aws

A Lambda Function URL can be created and configured via the AWS Lambda console or through the Lambda API. Upon creating a function URL, AWS Lambda automatically generates a unique URL endpoint for you that you can immediately use. Take note that this URL endpoint is static and doesn’t change once created. Lambda URLs are dual stack-enabled which support both IPv4 and IPv6 protocols, and can be invoked via a web browser, CURL, Postman, or any HTTP client.

Two authentication types for controlling access to a Lambda function URL:

  • AWS_IAM – uses IAM to authenticate and authorize users. Only IAM users or roles that have been granted permission to invoke the function through IAM policies will be able to do so.

  • NONE – allows anyone who has the function URL to execute the Lambda function, regardless of whether they have an AWS account or not.

Remember that you can access your function URL through the public Internet only. It’s not accessible via AWS PrivateLink. In addition, this feature uses resource-based policies for security and access control. You can further secure your function URL by enabling cross-origin resource sharing (CORS) to whitelist origins permitted to invoke it. A function URL can be applied to any Lambda function alias or to the $LATEST unpublished function version but not to any other function version.

AWS Lambda Function URL

 

Scenario

In this demo, let’s assume a scenario where you act as an administrator. Imagine you want to find information on EC2 instances with a specific tag directly from your team’s Slack channel. This way, you can seamlessly manage your AWS resources from any location (as long as you have access to Slack) without the need to navigate the AWS console or type CLI commands in the terminal. For the purposes of this demo, we’ll just retrieve the ID of instances and their current state.

I’ll take you through the steps of creating a custom command, setting up a Lambda function URL, and connecting the two to trigger the function when the command is submitted.

 

Tutorials dojo strip

Solution Overview

Build Your Own Slack commands with Lambda function URL

  1. A user enters a slash command (/list-instances <TAG_NAME>) in the Slack chat box. For example, the /list-instances DEV command would retrieve details on instances that were tagged as DEV. 

  2. Slack sends the command to the Lambda function URL via an HTTP POST request.

  3. The Lambda function receives the request, processes it, and generates a response.

  4. The response is sent back to Slack.

 

Before creating a Lambda Function URL, make sure you have the following already prepared:

  • A Slack workspace. You can create one here.

  • A Lambda function configured with Python as the runtime.

  • Ensure the following permission is attached to your Lambda function’s execution role. This is required so that Lambda can call the DescribeInstances API:

 

STEPS:

We’ll divide the steps into three main parts:

 

Creating a Lambda Function URL

  1. Open the console page of your Lambda function

  2. Under the Configuration tab, select Function URL, then click Create function URL

STEPS: We'll divide the steps into three main parts:  Creating a function URL  Creating a Slack application  Installing the Slack application  **Note: Ate Rachelle, i-a-anchor link po natin young tatlong parts.   Creating a function URL Open the console page of your Lambda function  Under the Configuration tab, select Function URL, then click Create function URL

3. We’re using the NONE auth type for demo purposes. Choose NONE. Hit Save.

Build Your Own Slack commands with Lambda function URL

When you create a function URL and choose the “NONE” auth type via the Console or AWS SAM, AWS automatically generates a resource-based policy for you and attaches it to your function. Take note that this is not the case when you’re creating function URLs through AWS CLI, CloudFormation, or the Lambda API.

4. Copy the code here and paste it into your Lambda code editor. Click Deploy.

Here’s a summary of what the code does when invoked:

  • Extracts payload from the event object.

  • The command issued from the Slack chat, along with details such as the Slack app ID, the requesting user, channel ID, and other things, is wrapped in a base64-encoded message in the body.

Build Your Own Slack commands with Lambda function URL

  • The parse_slack_payload() function decodes the body and converts the output (a list) into a dictionary so that we can retrieve data by key names.

  • Retrieves the tag value from the text key

  • The Lambda function then calls the DescribeInstances API using the tag value as a filter.

  • Formats the response and sends it back to Slack. 

Copy the URL of your Lambda function and paste it into a text editor. We’ll need this later.

 

Creating a Slack application

 

  1. First, sign in to your Slack workspace and create a new Slack application.

  2. Click Create New App

Build Your Own Slack commands with Lambda function URL

3. Select From scratch

Build Your Own Slack commands with Lambda function URL

4. Give your app a name and select a workspace. Click Create App.

Build Your Own Slack commands with Lambda function URL

5. Under Add features and functionality, click Slash commands.

Build Your Own Slack commands with Lambda function URL

AWS Exam Readiness Courses

6. Click Create New Command and enter the following details:

Build Your Own Slack commands with Lambda function URL

7. Hit Save

 

Installing the Slack application

  1. Under Settings, select Install App, then click Install to WorkSpace.

Build Your Own Slack commands with Lambda function URL

2. Click Allow.

Build Your Own Slack commands with Lambda function URL

Testing Your Lambda Function URL

That’s pretty much it! To test whether the application works, you must tag at least one EC2 instance in your account. Give it a tag name of Env and a value of DEV. Feel free to use any name/value pair you want but make sure you change the code as well. If you don’t have an existing instance, you may create a t2.micro to stay under the free tier.

Open up your Slack workspace and pick a channel where you’d run the command in. Type /list-instance DEV and press enter.

You should see a similar response to the GIF below:

Build Your Own Slack commands with Lambda function URL

 

Cleaning up

Once you’re finished, don’t forget to delete your Lambda function. You might accidentally post its URL somewhere on the internet. And since it’s publicly accessible, anyone invoking it can do so at unlimited times, which could lead to unexpected costs on your AWS Bill.

Wrapping up

I hope you enjoyed walking through the process of building a custom Slack command using AWS Lambda as the backend. The possibilities are endless with Lambda function URL, and this is just one example of its practical applications. You can use this same concept to create even more useful tools to help streamline your work and make your life easier. If you’re planning to implement this solution in production, it’s important to take a few extra steps to ensure the security of your function. One thing to consider is setting a reserved concurrency to protect against abuse. Additionally, adding some form of authentication in the code, such as verifying the requester’s identity by comparing your Slack app ID to the one received by the function, is a good start. Overall, the Lambda function URL is a powerful tool that can be used to create a variety of useful solutions. I hope you have fun exploring the possibilities and building your own custom Slack commands.

 

Learn Serverless and Get #AWSCertified 

AWS Lambda is the primary Function-as-a-Service(FaaS) of AWS. This is part of the serverless computing revolution that provides a highly scalable computing capacity with an extremely low cost. 

As more and more companies move their on-premises workloads to the cloud, the demand for AWS Certified professionals will continue to rise to fill the never-ending need for knowledgeable individuals. If you are a developer, you may want to consider taking the AWS Certified Developer Associate exam DVA-C02 to help your profile get noticed easily by headhunters and hiring managers. An AWS Certification can be your competitive edge against other well-capable professionals who are also vying for the same lucrative position you’re applying for. 

You can check out these FREE AWS Lambda Foundations digital course to get started:

AWS Lambda Function URL DVA-C02 examtopics

 

You can also try out this other free course to improve your knowledge of Serverless Development. More digital courses are available at this link at absolutely no cost.

 

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Tutorials Dojo portal

Learn AWS with our PlayCloud Hands-On Labs

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE AWS, Azure, GCP Practice Test Samplers

Follow Us On Linkedin

Recent Posts

Written by: Carlo Acebedo

Carlo is a cloud engineer and a content creator at Tutorials Dojo. He's also a member of the AWS Community builder and holds 5 AWS Certifications. Carlo specializes in building and automating solutions in the Amazon Web Services Cloud.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?