module cfnresponse doesn't exist for Python 3.12

0

Hi In my lambda code I am importing cfnresponse, which will be used in CloudFormation. I am getting the following error in my lambda implemented in Python 3.12 "Unable to import module '" : No module named 'cfnresponse'". I think, cfnresponse module is missing in Python 3.12. Please help me in resolving this issue. Thank you.

AWS
Kingson
asked 2 months ago392 views
1 Answer
2
Accepted Answer

Hello.

I tried running the CloudFormation template below and confirmed that Lambda was executed successfully.
I ran it in the Tokyo region.
It's possible that the issue is region-specific, so please try deploying the template below in the region you're using and see if you can reproduce the issue.

By the way, as commented in the URL below, "cfnresponse" can only be used when deploying Lambda and code using Zip with CloudFormation.
https://repost.aws/questions/QUtqrQ6_3zSxmKo3QtFW_4Aw/cfnresponse-package-is-missing-in-new-python-3-9-runtime#CO4K6fg1FGTMyq34g7Igzy_Q

AWSTemplateFormatVersion: "2010-09-09"

Resources:
  SampleLambda:
    Type: Custom::SampleLambda
    Properties:
      ServiceToken: !GetAtt "LambdaFunction.Arn"
      Hoge: "hoge"

  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Role: !GetAtt "LambdaExecutionRole.Arn"
      Runtime: "python3.12"
      Handler: index.lambda_handler
      Timeout: "180"
      Code:
        ZipFile: |
          import cfnresponse

          def lambda_handler(event, context):
              hoge = event['ResourceProperties']['Hoge']
              if event['RequestType'] == 'Create':
                  print('Create:'+hoge)
                  cfnresponse.send(event, context, cfnresponse.SUCCESS,
                                      {'Response': 'Success'})
              if event['RequestType'] == 'Delete':
                  print('Delete:'+hoge)
                  cfnresponse.send(event, context, cfnresponse.SUCCESS,
                                      {'Response': 'Success'})
              if event['RequestType'] == 'Update':
                  print('Update:'+hoge)
                  cfnresponse.send(event, context, cfnresponse.SUCCESS,
                                      {'Response': 'Success'})

  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: sample-lambda-policy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
profile picture
EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago
  • Thanks . I see cfnmodule is only available if we are using ZipFile

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions