https://pulumi.com logo
#general
Title
# general
f

full-plastic-13568

06/05/2023, 2:04 AM
Pulumi Python AWS: AttributeError: type object 'Output' has no attribute 'json_dumps' I use the Pulumi sample code at https://github.com/pulumi/examples/tree/master/aws-py-apigateway-lambda-serverless. pulumi version v3.69.0 It runs on AWS cloud. I got errors as follows: main.py", line 49, in body=pulumi.Output.json_dumps({ AttributeError: type object 'Output' has no attribute 'json_dumps' Here is the code:
Copy code
cat __main__.py
"""An AWS Python Pulumi program"""
import json
import iam
import pulumi
import pulumi_aws as aws

#region = 'us-west-2'
region = aws.config.region

custom_stage_name = 'example'

##################
## Lambda Function
##################

# Create a Lambda function, using code from the `./app` folder.

lambda_func = aws.lambda_.Function("mylambda",
    role=iam.lambda_role.arn,
    runtime="python3.7",
    handler="hello.handler",
    code=pulumi.AssetArchive({
        '.': pulumi.FileArchive('./hello_lambda')
    })
)


####################################################################
##
## API Gateway REST API (API Gateway V1 / original)
##    /{proxy+} - passes all requests through to the lambda function
##
####################################################################

# Create a single Swagger spec route handler for a Lambda function.
def swagger_route_handler(arn):
    return ({
        "x-amazon-apigateway-any-method": {
            "x-amazon-apigateway-integration": {
                "uri": pulumi.Output.format('arn:aws:apigateway:{0}:lambda:path/2015-03-31/functions/{1}/invocations', region, arn),
                "passthroughBehavior": "when_no_match",
                "httpMethod": "POST",
                "type": "aws_proxy",
            },
        },
    })

# Create the API Gateway Rest API, using a swagger spec.
rest_api = aws.apigateway.RestApi("api",
    body=pulumi.Output.json_dumps({
        "swagger": "2.0",
        "info": {"title": "api", "version": "1.0"},
        "paths": {
            "/{proxy+}": swagger_route_handler(lambda_func.arn),
        },
    }))

# Create a deployment of the Rest API.
deployment = aws.apigateway.Deployment("api-deployment",
    rest_api=rest_api.id,
    # Note: Set to empty to avoid creating an implicit stage, we'll create it
    # explicitly below instead.
    stage_name="",
)

# Create a stage, which is an addressable instance of the Rest API. Set it to point at the latest deployment.
stage = aws.apigateway.Stage("api-stage",
    rest_api=rest_api.id,
    deployment=deployment.id,
    stage_name=custom_stage_name,
)

# Give permissions from API Gateway to invoke the Lambda
rest_invoke_permission = aws.lambda_.Permission("api-rest-lambda-permission",
    action="lambda:invokeFunction",
    function=lambda_func.name,
    principal="<http://apigateway.amazonaws.com|apigateway.amazonaws.com>",
    source_arn=deployment.execution_arn.apply(lambda arn: arn + "*/*"),
)

#########################################################################
# Create an HTTP API and attach the lambda function to it
##    /{proxy+} - passes all requests through to the lambda function
##
#########################################################################

http_endpoint = aws.apigatewayv2.Api("http-api-pulumi-example",
    protocol_type="HTTP"
)

http_lambda_backend = aws.apigatewayv2.Integration("example",
    api_id=http_endpoint.id,
    integration_type="AWS_PROXY",
    connection_type="INTERNET",
    description="Lambda example",
    integration_method="POST",
    integration_uri=lambda_func.arn,
    passthrough_behavior="WHEN_NO_MATCH"
)

url = http_lambda_backend.integration_uri

http_route = aws.apigatewayv2.Route("example-route",
    api_id=http_endpoint.id,
    route_key="ANY /{proxy+}",
    target=http_lambda_backend.id.apply(lambda targetUrl: "integrations/" + targetUrl)
)

http_stage = aws.apigatewayv2.Stage("example-stage",
    api_id=http_endpoint.id,
    route_settings= [
        {
            "route_key": http_route.route_key,
            "throttling_burst_limit": 1,
            "throttling_rate_limit": 0.5,
        }
    ],
    auto_deploy=True
)

# Give permissions from API Gateway to invoke the Lambda
http_invoke_permission = aws.lambda_.Permission("api-http-lambda-permission",
    action="lambda:invokeFunction",
    function=lambda_func.name,
    principal="<http://apigateway.amazonaws.com|apigateway.amazonaws.com>",
    source_arn=http_endpoint.execution_arn.apply(lambda arn: arn + "*/*"),
)

# Export the https endpoint of the running Rest API
pulumi.export("apigateway-rest-endpoint", deployment.invoke_url.apply(lambda url: url + custom_stage_name + '/{proxy+}'))
# See "Outputs" for (Inputs and Outputs)[<https://www.pulumi.com/docs/intro/concepts/inputs-outputs/>] the usage of the pulumi.Output.all function to do string concatenation
pulumi.export("apigatewayv2-http-endpoint", pulumi.Output.all(http_endpoint.api_endpoint, http_stage.name).apply(lambda values: values[0] + '/' + values[1] + '/'))
Copy code
cat iam.py
# Copyright 2016-2018, Pulumi Corporation.  All rights reserved.

from pulumi_aws import iam

lambda_role = iam.Role('lambdaRole',
    assume_role_policy="""{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": "sts:AssumeRole",
                "Principal": {
                    "Service": "<http://lambda.amazonaws.com|lambda.amazonaws.com>"
                },
                "Effect": "Allow",
                "Sid": ""
            }
        ]
    }"""
)

lambda_role_policy = iam.RolePolicy('lambdaRolePolicy',
    role=lambda_role.id,
    policy="""{
        "Version": "2012-10-17",
        "Statement": [{
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }]
    }"""
)
Any one has idea?
b

billowy-army-68599

06/05/2023, 2:20 AM
You need to update your Pulumi SDK
f

full-plastic-13568

06/05/2023, 2:24 AM
The requirements.txt is cat requirements.txt pulumi>=3.5.1,<4.0.0 pulumi-aws>=5.0.0,<6.0.0 I did
Copy code
venv/bin/pip install -r requirements.txt
Requirement already satisfied: pulumi<4.0.0,>=3.5.1 in ./venv/lib/python3.9/site-packages (from -r requirements.txt (line 1)) (3.48.0)
Requirement already satisfied: pulumi-aws<6.0.0,>=5.0.0 in ./venv/lib/python3.9/site-packages (from -r requirements.txt (line 2)) (5.21.1)
Requirement already satisfied: grpcio==1.47 in ./venv/lib/python3.9/site-packages (from pulumi<4.0.0,>=3.5.1->-r requirements.txt (line 1)) (1.47.0)
Requirement already satisfied: semver~=2.8 in ./venv/lib/python3.9/site-packages (from pulumi<4.0.0,>=3.5.1->-r requirements.txt (line 1)) (2.13.0)
Requirement already satisfied: pyyaml~=6.0 in ./venv/lib/python3.9/site-packages (from pulumi<4.0.0,>=3.5.1->-r requirements.txt (line 1)) (6.0)
Requirement already satisfied: protobuf~=4.21 in ./venv/lib/python3.9/site-packages (from pulumi<4.0.0,>=3.5.1->-r requirements.txt (line 1)) (4.23.2)
Requirement already satisfied: six~=1.12 in ./venv/lib/python3.9/site-packages (from pulumi<4.0.0,>=3.5.1->-r requirements.txt (line 1)) (1.16.0)
Requirement already satisfied: dill~=0.3 in ./venv/lib/python3.9/site-packages (from pulumi<4.0.0,>=3.5.1->-r requirements.txt (line 1)) (0.3.6)
Requirement already satisfied: parver>=0.2.1 in ./venv/lib/python3.9/site-packages (from pulumi-aws<6.0.0,>=5.0.0->-r requirements.txt (line 2)) (0.4)
Requirement already satisfied: typing-extensions in ./venv/lib/python3.9/site-packages (from parver>=0.2.1->pulumi-aws<6.0.0,>=5.0.0->-r requirements.txt (line 2)) (4.6.3)
Requirement already satisfied: arpeggio>=1.7 in ./venv/lib/python3.9/site-packages (from parver>=0.2.1->pulumi-aws<6.0.0,>=5.0.0->-r requirements.txt (line 2)) (2.0.0)
Requirement already satisfied: attrs>=19.2 in ./venv/lib/python3.9/site-packages (from parver>=0.2.1->pulumi-aws<6.0.0,>=5.0.0->-r requirements.txt (line 2)) (23.1.0)
What SDK should I install?
@billowy-army-68599 Can you please share more clue?
@billowy-army-68599 Never mind. I figured it out. Here are what I did. pip install --upgrade pulumi pip install --upgrade pulumi_aws It solved the problem. Thanks a lot!
2 Views