Creating an Apache Airflow web login token - Amazon Managed Workflows for Apache Airflow

Creating an Apache Airflow web login token

You can use the commands on this page to generate a web login token, and then make Amazon Managed Workflows for Apache Airflow (MWAA) API calls directly in your command shell. For example, you can get a token, then deploy DAGs programmatically using Amazon MWAA APIs. The following section includes the steps to create an Apache Airflow web login token using the AWS CLI, a bash script, a POST API request, or a Python script. The token returned in the response is valid for 60 seconds.

Prerequisites

The following section describes the preliminary steps required to use the commands and scripts on this page.

Access

AWS CLI

The AWS Command Line Interface (AWS CLI) is an open source tool that enables you to interact with AWS services using commands in your command-line shell. To complete the steps on this page, you need the following:

Using the AWS CLI

The following example uses the create-web-login-token command in the AWS CLI to create an Apache Airflow web login token.

aws mwaa create-web-login-token --name YOUR_ENVIRONMENT_NAME

Using a bash script

The following example uses a bash script to call the create-web-login-token command in the AWS CLI to create an Apache Airflow web login token.

  1. Copy the contents of the following code sample and save locally as get-web-token.sh.

    #!/bin/bash HOST=YOUR_HOST_NAME YOUR_URL=https://$HOST/aws_mwaa/aws-console-sso?login=true# WEB_TOKEN=$(aws mwaa create-web-login-token --name YOUR_ENVIRONMENT_NAME --query WebToken --output text) echo $YOUR_URL$WEB_TOKEN
  2. Substitute the placeholders in red for YOUR_HOST_NAME and YOUR_ENVIRONMENT_NAME. For example, a host name for a public network may look like this (without the https://):

    123456a0-0101-2020-9e11-1b159eec9000.c2.us-east-1.airflow.amazonaws.com
  3. (optional) macOS and Linux users may need to run the following command to ensure the script is executable.

    chmod +x get-web-token.sh
  4. Run the following script to get a web login token.

    ./get-web-token.sh
  5. You should see the following in your command prompt:

    https://123456a0-0101-2020-9e11-1b159eec9000.c2.us-east-1.airflow.amazonaws.com/aws_mwaa/aws-console-sso?login=true#{your-web-login-token}

Using a POST API request

The following example uses a POST API request to create an Apache Airflow web login token.

  1. Copy the following URL and paste in the URL field of your REST API client.

    https://YOUR_HOST_NAME/aws_mwaa/aws-console-sso?login=true#WebToken
  2. Substitute the placeholders in red for YOUR_HOST_NAME. For example, a host name for a public network may look like this (without the https://):

    123456a0-0101-2020-9e11-1b159eec9000.c2.us-east-1.airflow.amazonaws.com
  3. Copy the following JSON and paste in the body field of your REST API client.

    { "name": "YOUR_ENVIRONMENT_NAME" }
  4. Substitute the placeholder in red for YOUR_ENVIRONMENT_NAME.

  5. Add key-value pairs in the authorization field. For example, if you're using Postman, choose AWS Signature, and then enter your:

    • AWS_ACCESS_KEY_ID in AccessKey

    • AWS_SECRET_ACCESS_KEY in SecretKey

  6. You should see the following response:

    { "webToken": "<Short-lived token generated for enabling access to the Apache Airflow Webserver UI>", "webServerHostname": "<Hostname for the WebServer of the environment>" }

Using a Python script

The following example uses the boto3 create_web_login_token method in a Python script to create an Apache Airflow web login token. You can run this script outside of Amazon MWAA. The only thing you need to do is install the boto3 library. You may want to create a virtual environment to install the library. It assumes you have configured AWS authentication credentials for your account.

  1. Copy the contents of the following code sample and save locally as create-web-login-token.py.

    import boto3 mwaa = boto3.client('mwaa') response = mwaa.create_web_login_token( Name="YOUR_ENVIRONMENT_NAME" ) webServerHostName = response["WebServerHostname"] webToken = response["WebToken"] airflowUIUrl = 'https://{0}/aws_mwaa/aws-console-sso?login=true#{1}'.format(webServerHostName, webToken) print("Here is your Airflow UI URL: ") print(airflowUIUrl)
  2. Substitute the placeholder in red for YOUR_ENVIRONMENT_NAME.

  3. Run the following script to get a web login token.

    python3 create-web-login-token.py

What's next?

  • Explore the Amazon MWAA API operation used to create a web login token at CreateWebLoginToken.