An AWS Lambda execution role gives a function temporary credentials for AWS API operations. Its permissions are not inherited from the human who deployed the function. This distinction becomes important when Lambda queries Amazon Athena, because a working query may require access to Athena API actions, a permitted workgroup, AWS Glue metadata, the underlying Amazon S3 source objects and the query-results location.
This walkthrough uses a disposable, harmless dataset to explain the authorization path. The goal is to configure the smallest practical permission set, test success and denial, and understand why an apparently authorized Athena request can still fail at the data or results layer.
Understand the four principals and resources
The Lambda service assumes an IAM execution role with a trust policy permitting lambda.amazonaws.com. The deployment identity creates or updates function configuration; it does not automatically supply runtime data permissions. Athena processes a request under the caller’s permissions, while the S3 bucket and Glue catalog may enforce additional resource access controls.
For an approved query, identify the Athena workgroup, catalog, database and table, S3 location holding source data, and S3 output location for results. These are distinct resources. A function allowed to call StartQueryExecution can still be unable to read table objects or write query results.
A resource’s own policy, KMS key policy, permission boundary or organizational service control policy can further restrict effective access. Widening one inline IAM statement may not repair a denial enforced by another layer. Capture the error and test the actual principal before changing permissions.
Create an isolated query dataset
Choose an authorized AWS account and Region. Create an S3 prefix with a few non-sensitive sample rows, then register an appropriate Glue Data Catalog table through supported Athena tooling. Use predictable test data and document the schema. A lab should never copy production customer information into a public or broadly accessible bucket.
Create or select an Athena workgroup under the intended governance settings and define where results will be stored. Review workgroup engine version, query settings, output encryption and data-access policy. The results bucket may need a separate prefix or policy from the source dataset.
Test the query manually with an authorized principal before automating it. A simple aggregate over a handful of test records is enough to confirm the schema. If the query fails here, adding Lambda is unlikely to solve the underlying table or permission problem.
Create a dedicated Lambda execution role
The trust relationship should allow Lambda to assume the role; it should not generally allow unrelated AWS services to use those credentials. Add only the required logging permissions and narrowly scoped Athena actions needed for query execution and status inspection. Review workgroup resource scope and any action that AWS cannot constrain to a specific resource.
Grant the required Glue catalog read actions for the named database and table and appropriate S3 read permissions for the source. For query output, the role also needs suitable S3 write or read permissions, depending on the workflow. If KMS-encrypted data or results are involved, examine the key policy and specific cryptographic permissions required by the service.
Avoid the shortcut of AmazonS3FullAccess and broad Athena permissions in a permanent application role. A policy granting all S3 access might make a demo work, but it conceals the exact resources the function needs and increases the damage possible from compromised function code.
Implement an asynchronous query workflow
A Lambda handler can call Athena StartQueryExecution using the intended WorkGroup, QueryString and QueryExecutionContext. That API starts a query and returns a query execution identifier; it does not mean the result is immediately available. The function must inspect status through GetQueryExecution, then retrieve the result after successful completion.
If the query runs longer than the allowed function duration, do not create an unlimited polling loop. Consider a workflow coordinator, asynchronous job tracking, or other supported state management to check completion later. Respect service quotas, retry transient errors and define a deadline for an operation that remains queued or running.
Build query text safely. Avoid interpolating untrusted user input into raw SQL. Validate any permitted parameters and restrict tables and workgroups so a function cannot become an arbitrary data-exfiltration interface simply because its execution role has query privileges.
Test success and permission denials
Run a harmless query against the named table and inspect the returned execution ID, final state and small result. Then deliberately remove S3 source permission in a test role. Athena may accept an API request but fail when it cannot access the underlying data. Restore the permission and observe the difference.
Repeat with a missing Glue catalog permission, a disallowed workgroup, an incorrect output prefix or an unavailable KMS key grant. Each failure teaches a separate authorization dependency. Keep a record of the actual AWS error and policy used instead of assuming every AccessDeniedException originates in the same service.
Verify that an unauthorized test principal cannot invoke the function if invocation is meant to be restricted. Lambda resource policies and invocation authorization are separate from the function’s execution role. Giving a caller permission to invoke does not grant it the Lambda role’s privileges.
Make logging and cleanup appropriate to the risk
Record query execution IDs, timing, high-level status and controlled error codes. Do not log sensitive SQL parameters, secret credentials or full query results by default. Use CloudWatch log retention and an incident owner according to the organization’s policy.
Set budgets for Athena scans, results storage and any continued scheduled execution. Query design and data format can materially affect scanned bytes. A short demonstration can use a small partitioned or columnar dataset to learn why query patterns influence cost.
The Lambda and Secrets Manager example explores another least-privilege integration, while centralized CloudTrail logging provides context for AWS API evidence. Neither implies that CloudTrail captures the full contents of every SQL result.
After testing, remove unused queries, result objects, test table and bucket contents, Lambda function and dedicated role under an approved cleanup procedure. The skill demonstrated is not merely ‘attach IAM to Lambda’; it is tracing authorization across the entire query and output path.