示例#1
0
def install(ctx, **kwargs):
    """Install New Relic AWS Lambda Integration"""
    input = IntegrationInstall(session=None, verbose=ctx.obj["VERBOSE"], **kwargs)

    input = input._replace(
        session=boto3.Session(
            profile_name=input.aws_profile, region_name=input.aws_region
        )
    )

    if not input.linked_account_name:
        input = input._replace(
            linked_account_name=(
                "New Relic Lambda Integration - %s"
                % integrations.get_aws_account_id(input.session)
            )
        )

    if input.aws_permissions_check:
        permissions.ensure_integration_install_permissions(input)

    click.echo("Validating New Relic credentials")
    gql_client = api.validate_gql_credentials(input)

    click.echo("Retrieving integration license key")
    nr_license_key = api.retrieve_license_key(gql_client)

    click.echo("Checking for a pre-existing link between New Relic and AWS")
    integrations.validate_linked_account(gql_client, input)

    install_success = True

    click.echo("Creating the AWS role for the New Relic AWS Lambda Integration")
    role = integrations.create_integration_role(input)
    install_success = install_success and role

    if role:
        click.echo("Linking New Relic account to AWS account")
        res = api.create_integration_account(gql_client, input, role)
        install_success = res and install_success

        click.echo("Enabling Lambda integration on the link between New Relic and AWS")
        res = api.enable_lambda_integration(gql_client, input)
        install_success = res and install_success

    if input.enable_license_key_secret:
        click.echo("Creating the managed secret for the New Relic License Key")
        res = integrations.install_license_key(input, nr_license_key)
        install_success = install_success and res

    if input.enable_cw_ingest:
        click.echo("Creating newrelic-log-ingestion Lambda function in AWS account")
        res = integrations.install_log_ingestion(input, nr_license_key)
        install_success = res and install_success

    if install_success:
        done("Install Complete")

        if input.verbose:
            click.echo(
                "\nNext steps: Add the New Relic layers to your Lambda functions with "
                "the below command.\n"
            )
            command = [
                "$",
                "newrelic-lambda",
                "layers",
                "install",
                "--function",
                "all",
                "--nr-account-id",
                input.nr_account_id,
            ]
            if input.aws_profile:
                command.append("--aws-profile %s" % input.aws_profile)
            if input.aws_region:
                command.append("--aws-region %s" % input.aws_region)
            click.echo(" ".join(command))
    else:
        failure("Install Incomplete. See messages above for details.", exit=True)
def test_get_aws_account_id(aws_credentials):
    session = boto3.Session(region_name="us-east-1")
    assert get_aws_account_id(session) == "123456789012"