Blog | G5 Cyber Security

AWS AppSync: IAM vs API Key Security

TL;DR

IAM authorization in AWS AppSync is significantly more secure than API key-based authorization because it relies on AWS’s robust identity and access management system. API keys are static secrets that can be compromised, while IAM uses dynamic permissions tied to users or roles.

Why API Key Authorization Isn’t Enough

API Keys are simple to set up but offer limited security. Here’s why:

How IAM Authorization Works

IAM authorization integrates with AWS Identity and Access Management (IAM) to control access to your AppSync GraphQL API. Here’s how it works:

  1. AWS Users & Roles: You define users or roles in IAM, granting them specific permissions.
  2. Cognito User Pools (Optional): Integrate with Cognito for user authentication and authorization. AppSync can use the Cognito identity to determine IAM policies.
  3. IAM Policies: These JSON documents specify what actions a user/role is allowed to perform on your AppSync API resources (queries, mutations, subscriptions).
  4. Request Interception: When a request comes in, AppSync checks the caller’s identity against IAM policies.
  5. Dynamic Permissions: Access is granted or denied based on the user’s/role’s permissions at runtime.

Setting up IAM Authorization – Step-by-Step

  1. Create an IAM Role: Create a role that will be assumed by your AppSync service.
    aws iam create-role --role-name AppSyncServiceRole --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"appsync.amazonaws.com"},"Action":"sts:AssumeRole"]}'
  2. Attach Policies to the Role: Attach policies that grant access to the resources your AppSync API needs (e.g., DynamoDB tables).
    aws iam attach-role-policy --role-name AppSyncServiceRole --policy-arn arn:aws:iam::YOUR_ACCOUNT_ID:policy/DynamoDBFullAccess
  3. Configure the IAM Authorization in AppSync: In your AppSync API settings, enable IAM authorization for the appropriate resolvers.
    • Go to your AppSync console.
    • Select your API and navigate to ‘Settings’.
    • Under ‘Authentication’, choose ‘IAM’.
    • Configure a resolver mapping template that uses the identity information from the request context. For example, in VTL:
      #if($context.identity) $util.toJson($context.identity) #else {} #end
  4. Update your GraphQL Schema: Add directives to control access at the field level.
    type Query { 
      getItem(id: ID!): Item @auth(cognito: ["YOUR_COGNITO_USER_POOL_ID"])
    }
  5. Test Access: Use a user with the appropriate IAM permissions to test access to your API.

Benefits of IAM Authorization

Exit mobile version