Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
Yandex Serverless Containers
  • Comparison with other Yandex Cloud services
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Getting an IAM token for a service account using a container
    • Getting information about a container
    • Managing rights to access a container
    • Creating a container
    • Invoking a container
    • Managing a container
    • Getting information about a trigger
    • Creating a trigger
    • Managing triggers
  • Concepts
  • Practical guidelines
  • Access management
  • Pricing policy
  • Containers API reference
  • API Triggers reference
  • Questions and answers
  1. Step-by-step instructions
  2. Getting an IAM token for a service account using a container

Getting an IAM token for a service account using a container

Written by
Yandex Cloud

    If the container revision was created with a service account, you can get an IAM token from the metadata service in Google Compute Engine format using the API.

    To do this, within the container, run a GET request at http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token and specify the HTTP header Metadata-Flavor: Google.

    Below is an example of a function for obtaining an IAM token.

    JavaScript
    const fetch = require("node-fetch");
    let url = 'http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token';
    let headers = {'Metadata-Flavor': 'Google'};
    
    async function getToken(event) {
        const resp = await fetch(url, {
            headers: headers,
        });
        let respJson = await resp.json();
        return {
            token: respJson['access_token'],
            expiresInSeconds: respJson['expires_in'],
        };
    };
    

    Was the article helpful?

    Language / Region
    Yandex project
    © 2023 Yandex.Cloud LLC